<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-OyDaGkZ9eq">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="ElasticSearch入门篇">
<meta property="og:type" content="article">
<meta property="og:title" content="ElasticSearch 使用">
<meta property="og:url" content="http://example.com/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch%E6%96%87%E6%A1%A3/index.html">
<meta property="og:site_name" content="极风键客">
<meta property="og:description" content="ElasticSearch入门篇">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://images2015.cnblogs.com/blog/980882/201702/980882-20170208171207963-1711795457.png">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/F967846635974B32A8D490508E781F00?method=download&shareKey=2ad37f2dc1cc016e1afe3a0849046cef">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/F432E6405D5C4D5599A80F3F2F0FEB83?method=download&shareKey=242de0ee6034de7f0e46c6c120d88e68">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/A03FC0640DD043EBBAFF66A34CB4B225?method=download&shareKey=073d77cddf7efa2810059f5b591b3548">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/EA3ED55EB0ED40C683112AC6ED8716AE?method=download&shareKey=7517e79986e6585de886c59966057d9c">
<meta property="og:image" content="https://images2017.cnblogs.com/blog/210978/201708/210978-20170805113725272-708617928.png">
<meta property="og:image" content="http://img.my.csdn.net/uploads/201209/10/1347267642_4728.jpg">
<meta property="og:image" content="http://img.my.csdn.net/uploads/201202/13/0_1329141580k34Q.gif">
<meta property="og:image" content="http://img.my.csdn.net/uploads/201209/10/1347267682_4417.jpg">
<meta property="article:published_time" content="2021-01-04T12:46:25.000Z">
<meta property="article:modified_time" content="2021-02-17T07:40:31.758Z">
<meta property="article:author" content="王增明">
<meta property="article:tag" content="java">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="数据库">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://images2015.cnblogs.com/blog/980882/201702/980882-20170208171207963-1711795457.png">

<link rel="canonical" href="http://example.com/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch%E6%96%87%E6%A1%A3/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>ElasticSearch 使用 | 极风键客</title>
  


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b94f963e94e4127fa71ab49cbddce93b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="极风键客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">极风键客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">技术狂热爱好者!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbmd6ZW5nbWluZw==" title="客官,来嘛~" aria-label="客官,来嘛~"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch%E6%96%87%E6%A1%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="王增明">
      <meta itemprop="description" content="喜欢折腾的极客聚集地.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="极风键客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ElasticSearch 使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-01-04 20:46:25" itemprop="dateCreated datePublished" datetime="2021-01-04T20:46:25+08:00">2021-01-04</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-02-17 15:40:31" itemprop="dateModified" datetime="2021-02-17T15:40:31+08:00">2021-02-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ES/" itemprop="url" rel="index"><span itemprop="name">ES</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>64k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>59 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>ElasticSearch入门篇</p>
<a id="more"></a>

<h1 id="第一章-ElasticSearch入门篇"><a href="#第一章-ElasticSearch入门篇" class="headerlink" title="第一章 ElasticSearch入门篇"></a>第一章 ElasticSearch入门篇</h1><h2 id="第一节-ElasticSearch概述"><a href="#第一节-ElasticSearch概述" class="headerlink" title="第一节 ElasticSearch概述"></a>第一节 ElasticSearch概述</h2><h3 id="1-1-ElasticSearch-简介"><a href="#1-1-ElasticSearch-简介" class="headerlink" title="1.1 ElasticSearch 简介"></a>1.1 ElasticSearch 简介</h3><p>​    是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎，基于RESTfulweb接口。ElasticSearch是用Java开发的，并作为Apache许可条款下的开放源码发布，是当前流行的企业级搜索引擎。设计用于云计算中，能够达到实时搜索，稳定，可靠，快速，安装使用方便。构建在全文检索开源软件Lucene之上的Elasticsearch，不仅能对海量规模的数据完成分布式索引与检索，还能提供数据聚合分析。据国际权威的数据库产品评测机构DBEngines的统计，在2016年1月，Elasticsearch已超过Solr等，成为排名第一的搜索引擎类应用</p>
<p>概括：基于Restful标准的高扩展高可用的实时数据分析的全文搜索工具</p>
<h3 id="1-2ElasticSearch的基本概念"><a href="#1-2ElasticSearch的基本概念" class="headerlink" title="1.2ElasticSearch的基本概念"></a>1.2ElasticSearch的基本概念</h3><p>**Index:**索引–类似于mysql数据库中的database</p>
<p>**Type:**类型–类似于mysql数据库中的table表，es中可以在Index中建立type（table），通过mapping进行映射。　　</p>
<p>**Document:**文档–由于es存储的数据是文档型的，一条数据对应一篇文档即相当于mysql数据库中的一行数据row，一个文档中可以有多个字段也就是mysql数据库一行可以有多列。<br>**Field:**es中一个文档中对应的多个列与mysql数据库中每一列对应</p>
<p>**Mapping:**可以理解为mysql或者solr中对应的schema，只不过有些时候es中的mapping增加了动态识别功能，感觉很强大的样子，其实实际生产环境上不建议使用，最好还是开始制定好了对应的schema为主。
　　</p>
<p>indexed</p>
<p>　　就是名义上的建立索引。mysql中一般会对经常使用的列增加相应的索引用于提高查询速度，而在es中默认都是会加上索引的，除非你特殊制定不建立索引只是进行存储用于展示，这个需要看你具体的需求和业务进行设定了。</p>
<p>Query DSL</p>
<p>　　类似于mysql的sql语句，只不过在es中是使用的json格式的查询语句，专业术语就叫：QueryDSL</p>
<p>GET/PUT/POST/DELETE</p>
<p>　　分别类似与mysql中的select/update/delete……
　　
　　</p>
<h3 id="1-3Elasticsearch的架构"><a href="#1-3Elasticsearch的架构" class="headerlink" title="1.3Elasticsearch的架构"></a>1.3Elasticsearch的架构</h3><p><img src="https://images2015.cnblogs.com/blog/980882/201702/980882-20170208171207963-1711795457.png" alt="image"></p>
<p>Gateway层</p>
<p>es用来存储索引文件的一个文件系统且它支持很多类型，例如：本地磁盘、共享存储（做snapshot的时候需要用到）、hadoop的hdfs分布式存储、亚马逊的S3。它的主要职责是用来对数据进行长持久化以及整个集群重启之后可以通过gateway重新恢复数据。</p>
<p>Distributed Lucene Directory</p>
<p>Gateway上层就是一个lucene的分布式框架，lucene是做检索的，但是它是一个单机的搜索引擎，像这种es分布式搜索引擎系统，虽然底层用lucene，但是需要在每个节点上都运行lucene进行相应的索引、查询以及更新，所以需要做成一个分布式的运行框架来满足业务的需要。</p>
<p>四大模块组件</p>
<p>districted lucene directory之上就是一些es的模块，Index Module是索引模块，就是对数据建立索引也就是通常所说的建立一些倒排索引等；Search Module是搜索模块，就是对数据进行查询搜索；Mapping模块是数据映射与解析模块，就是你的数据的每个字段可以根据你建立的表结构通过mapping进行映射解析，如果你没有建立表结构，es就会根据你的数据类型推测你的数据结构之后自己生成一个mapping，然后都是根据这个mapping进行解析你的数据；River模块在es2.0之后应该是被取消了，它的意思表示是第三方插件，例如可以通过一些自定义的脚本将传统的数据库（mysql）等数据源通过格式化转换后直接同步到es集群里，这个river大部分是自己写的，写出来的东西质量参差不齐，将这些东西集成到es中会引发很多内部bug，严重影响了es的正常应用，所以在es2.0之后考虑将其去掉。</p>
<p>Discovery、Script</p>
<p>es4大模块组件之上有 Discovery模块：es是一个集群包含很多节点，很多节点需要互相发现对方，然后组成一个集群包括选主的，这些es都是用的discovery模块，默认使用的是 Zen，也可是使用EC2；es查询还可以支撑多种script即脚本语言，包括mvel、js、python等等。</p>
<p> Transport协议层</p>
<p>再上一层就是es的通讯接口Transport，支持的也比较多：Thrift、Memcached以及Http，默认的是http，JMX就是java的一个远程监控管理框架，因为es是通过java实现的。</p>
<p>RESTful接口层</p>
<p>最上层就是es暴露给我们的访问接口，官方推荐的方案就是这种Restful接口，直接发送http请求，方便后续使用nginx做代理、分发包括可能后续会做权限的管理，通过http很容易做这方面的管理。如果使用java客户端它是直接调用api，在做负载均衡以及权限管理还是不太好做。</p>
<h3 id="1-4RESTfull-API"><a href="#1-4RESTfull-API" class="headerlink" title="1.4RESTfull API"></a>1.4RESTfull API</h3><p>一种软件架构风格、设计风格，而不是标准，只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁，更有层次，更易于实现缓存等机制。在目前主流的三种Web服务交互方案中，REST相比于SOAP（Simple Object Access protocol，简单对象访问协议）以及XML-RPC更加简单明了</p>
<p>(Representational State Transfer</p>
<p>意思是：表述性状态传递)</p>
<p>它使用典型的HTTP方法，诸如GET,POST.DELETE,PUT来实现资源的获取，添加，修改，删除等操作。即通过HTTP动词来实现资源的状态扭转<br>复制代码</p>
<p>GET 用来获取资源</p>
<p>POST 用来新建资源（也可以用于更新资源）</p>
<p>PUT 用来更新资源</p>
<p>DELETE 用来删除资源</p>
<h3 id="1-5CRUL命令"><a href="#1-5CRUL命令" class="headerlink" title="1.5CRUL命令"></a>1.5CRUL命令</h3><p>以命令的方式执行HTTP协议的请求<br>GET/POST/PUT/DELETE</p>
<p>示例：<br>访问一个网页</p>
<p>curl <span class="exturl" data-url="aHR0cDovL3d3dy5iYWlkdS5jb20v">www.baidu.com<i class="fa fa-external-link-alt"></i></span></p>
<p>curl -o tt.html <span class="exturl" data-url="aHR0cDovL3d3dy5iYWlkdS5jb20v">www.baidu.com<i class="fa fa-external-link-alt"></i></span></p>
<p>显示响应的头信息</p>
<p>curl -i <span class="exturl" data-url="aHR0cDovL3d3dy5iYWlkdS5jb20v">www.baidu.com<i class="fa fa-external-link-alt"></i></span></p>
<p>显示一次HTTP请求的通信过程</p>
<p>curl -v <span class="exturl" data-url="aHR0cDovL3d3dy5iYWlkdS5jb20v">www.baidu.com<i class="fa fa-external-link-alt"></i></span></p>
<p>执行GET/POST/PUT/DELETE操作</p>
<p>curl -X GET/POST/PUT/DELETE url</p>
<h3 id="1-6CentOS7下安装ElasticSearch6-2-4"><a href="#1-6CentOS7下安装ElasticSearch6-2-4" class="headerlink" title="1.6CentOS7下安装ElasticSearch6.2.4"></a>1.6CentOS7下安装ElasticSearch6.2.4</h3><p>(1)配置JDK环境</p>
<p> 配置环境变量</p>
<p>export JAVA_HOME=”/opt/jdk1.8.0_144”</p>
<p>export PATH=”$JAVA_HOME/bin:$PATH”</p>
<p>export CLASSPATH=”.:$JAVA_HOME/lib”</p>
<p>(2)安装ElasticSearch6.2.4</p>
<p>下载地址：<span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9jbi9kb3dubG9hZHMvZWxhc3RpY3NlYXJjaA==">https://www.elastic.co/cn/downloads/elasticsearch<i class="fa fa-external-link-alt"></i></span></p>
<p>启动报错：<br><img src="https://note.youdao.com/yws/api/personal/file/F967846635974B32A8D490508E781F00?method=download&shareKey=2ad37f2dc1cc016e1afe3a0849046cef" alt="image"></p>
<p>解决方式：<br>bin/elasticsearch -Des.insecure.allow.root=true</p>
<p>或者修改bin/elasticsearch，加上ES_JAVA_OPTS属性：<br>ES_JAVA_OPTS=”-Des.insecure.allow.root=true”</p>
<p>再次启动：<br><img src="https://note.youdao.com/yws/api/personal/file/F432E6405D5C4D5599A80F3F2F0FEB83?method=download&shareKey=242de0ee6034de7f0e46c6c120d88e68" alt="image"></p>
<p>这是出于系统安全考虑设置的条件。由于ElasticSearch可以接收用户输入的脚本并且执行，为了系统安全考   虑，建议创建一个单独的用户用来运行ElasticSearch。</p>
<p>创建用户组和用户：</p>
<p>groupadd esgroup</p>
<p>useradd esuser -g esgroup -p espassword</p>
<p>更改elasticsearch文件夹及内部文件的所属用户及组：</p>
<p>cd /opt</p>
<p>chown -R esuser:esgroup elasticsearch-6.2.4</p>
<p>切换用户并运行：</p>
<p>su esuser</p>
<p>./bin/elasticsearch</p>
<p>再次启动显示已杀死：<br><img src="https://note.youdao.com/yws/api/personal/file/A03FC0640DD043EBBAFF66A34CB4B225?method=download&shareKey=073d77cddf7efa2810059f5b591b3548" alt="image"></p>
<p>需要调整JVM的内存大小：</p>
<p>vi bin/elasticsearch</p>
<p>ES_JAVA_OPTS=”-Xms512m -Xmx512m”</p>
<p>再次启动：启动成功</p>
<p>如果显示如下类似信息：</p>
<p>[INFO ][o.e.c.r.a.DiskThresholdMonitor] [ZAds5FP] low disk watermark [85%] exceeded on     [ZAds5FPeTY-ZUKjXd7HJKA][ZAds5FP][/opt/elasticsearch-6.2.4/data/nodes/0] free: 1.2gb[14.2%],     replicas will not be assigned to this node</p>
<p>需要清理磁盘空间。</p>
<p>后台运行：./bin/elasticsearch -d</p>
<p>测试连接：curl 127.0.0.1:9200</p>
<p>会看到一下JSON数据：<br> [root@localhost ~]# curl 127.0.0.1:9200<br>  {<br>  “name” : “rBrMTNx”,<br>  “cluster_name” : “elasticsearch”,<br>  “cluster_uuid” : “-noR5DxFRsyvAFvAzxl07g”,<br>  “version” : {<br>    “number” : “5.1.1”,<br>    “build_hash” : “5395e21”,<br>    “build_date” : “2016-12-06T12:36:15.409Z”,<br>    “build_snapshot” : false,<br>    “lucene_version” : “6.3.0”<br>  },<br>  “tagline” : “You Know, for Search”<br> }</p>
<p>实现远程访问：<br>需要对config/elasticsearch.yml进行   配置：<br>    network.host: 192.168.25.131</p>
<p>再次启动报错：<br><img src="https://note.youdao.com/yws/api/personal/file/EA3ED55EB0ED40C683112AC6ED8716AE?method=download&shareKey=7517e79986e6585de886c59966057d9c" alt="image"></p>
<p>处理第一个错误：</p>
<p>vim /etc/security/limits.conf       //文件最后加入</p>
<p>esuser soft nofile 65536</p>
<p>esuser hard nofile 65536</p>
<p>esuser soft nproc 4096</p>
<p>esuser hard nproc 4096</p>
<p>处理第二个错误：</p>
<p>进入limits.d目录下修改配置文件。</p>
<p>vim /etc/security/limits.d/20-nproc.conf<br>修改为 esuser soft nproc 4096</p>
<p>处理第三个错误：</p>
<p>vim /etc/sysctl.conf</p>
<p>vm.max_map_count=655360</p>
<p>执行以下命令生效：<br>sysctl -p</p>
<p>关闭防火墙：systemctl stop firewalld.service</p>
<p>再次启动成功！</p>
<h3 id="1-7安装Head插件"><a href="#1-7安装Head插件" class="headerlink" title="1.7安装Head插件"></a>1.7安装Head插件</h3><p>Head是elasticsearch的集群管理工具，可以用于数据的浏览和查询</p>
<p>(1)elasticsearch-head是一款开源软件，被托管在github上面，所以如果我们要使用它，必须先安装git，通过git获取elasticsearch-head</p>
<p>(2)运行elasticsearch-head会用到grunt，而grunt需要npm包管理器，所以nodejs是必须要安装的</p>
<p>(3)elasticsearch5.0之后，elasticsearch-head不做为插件放在其plugins目录下了。<br>使用git拷贝elasticsearch-head到本地</p>
<p>cd /usr/local/</p>
<p> git clone git://github.com/mobz/elasticsearch-head.git</p>
<p>(4)安装elasticsearch-head依赖包</p>
<p>[root@localhost local]# npm install -g grunt-cli</p>
<p>[root@localhost _site]# cd /usr/local/elasticsearch-head/</p>
<p>[root@localhost elasticsearch-head]# cnpm install</p>
<p>(5)修改Gruntfile.js</p>
<p>[root@localhost _site]# cd /usr/local/elasticsearch-head/</p>
<p>[root@localhost elasticsearch-head]# vi Gruntfile.js</p>
<p>在connect–&gt;server–&gt;options下面添加：hostname:’*’，允许所有IP可以访问</p>
<p>(6)修改elasticsearch-head默认连接地址<br>[root@localhost elasticsearch-head]# cd /usr/local/elasticsearch-head/_site/</p>
<p>[root@localhost _site]# vi app.js</p>
<p>将this.base_uri = this.config.base_uri || this.prefs.get(“app-base_uri”) || “<span class="exturl" data-url="aHR0cDovL2xvY2FsaG9zdDo5MjAwJnF1b3Q7O+S4reeahGxvY2FsaG9zdOS/ruaUueaIkOS9oGVz55qE5pyN5Yqh5Zmo5Zyw5Z2A">http://localhost:9200&quot;;中的localhost修改成你es的服务器地址<i class="fa fa-external-link-alt"></i></span></p>
<p>(7)配置elasticsearch允许跨域访问</p>
<p>打开elasticsearch的配置文件elasticsearch.yml，在文件末尾追加下面两行代码即可：</p>
<p>http.cors.enabled: true</p>
<p>http.cors.allow-origin: “*”</p>
<p>(8)打开9100端口</p>
<p>[root@localhost elasticsearch-head]# firewall-cmd –zone=public –add-port=9100/tcp –permanent</p>
<p>重启防火墙</p>
<p>[root@localhost elasticsearch-head]# firewall-cmd –reload</p>
<p>(9)启动elasticsearch</p>
<p>(10)启动elasticsearch-head</p>
<p>[root@localhost _site]# cd /usr/local/elasticsearch-head/</p>
<p>[root@localhost elasticsearch-head]# node_modules/grunt/bin/grunt server</p>
<p>(11)访问elasticsearch-head</p>
<p>关闭防火墙：systemctl stop firewalld.service</p>
<p>浏览器输入网址：<span class="exturl" data-url="aHR0cDovLzE5Mi4xNjguMjUuMTMxOjkxMDAv">http://192.168.25.131:9100/<i class="fa fa-external-link-alt"></i></span></p>
<h3 id="1-8安装Kibana"><a href="#1-8安装Kibana" class="headerlink" title="1.8安装Kibana"></a>1.8安装Kibana</h3><p>Kibana是一个针对Elasticsearch的开源分析及可视化平台，使用Kibana可以查询、查看并与存储在ES索引的数据进行交互操作，使用Kibana能执行高级的数据分析，并能以图表、表格和地图的形式查看数据</p>
<p>(1)下载Kibana<br><span class="exturl" data-url="aHR0cHM6Ly93d3cuZWxhc3RpYy5jby9kb3dubG9hZHMva2liYW5h">https://www.elastic.co/downloads/kibana<i class="fa fa-external-link-alt"></i></span></p>
<p>(2)把下载好的压缩包拷贝到/soft目录下</p>
<p>(3)解压缩，并把解压后的目录移动到/user/local/kibana</p>
<p>(4)编辑kibana配置文件</p>
<p>[root@localhost /]# vi /usr/local/kibana/config/kibana.yml</p>
<p><img src="https://images2017.cnblogs.com/blog/210978/201708/210978-20170805113725272-708617928.png" alt="image"></p>
<p>将server.host,elasticsearch.url修改成所在服务器的ip地址</p>
<p>(5)开启5601端口</p>
<p>Kibana的默认端口是5601</p>
<p>开启防火墙:systemctl start firewalld.service</p>
<p>开启5601端口:firewall-cmd –permanent –zone=public –add-port=5601/tcp</p>
<p>重启防火墙：firewall-cmd –reload</p>
<p>(6)启动Kibana</p>
<p>[root@localhost /]# /usr/local/kibana/bin/kibana</p>
<p>浏览器访问：<span class="exturl" data-url="aHR0cDovLzE5Mi4xNjguMjUuMTMxOjU2MDEv">http://192.168.25.131:5601<i class="fa fa-external-link-alt"></i></span></p>
<h3 id="1-9安装中文分词器"><a href="#1-9安装中文分词器" class="headerlink" title="1.9安装中文分词器"></a>1.9安装中文分词器</h3><p>(1)下载中文分词器<br><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL21lZGNsL2VsYXN0aWNzZWFyY2gtYW5hbHlzaXMtaWs=">https://github.com/medcl/elasticsearch-analysis-ik<i class="fa fa-external-link-alt"></i></span></p>
<pre><code>下载elasticsearch-analysis-ik-master.zip
</code></pre>
<p>(2)解压elasticsearch-analysis-ik-master.zip</p>
<p>   unzip elasticsearch-analysis-ik-master.zip</p>
<p>(3)进入elasticsearch-analysis-ik-master，编译源码</p>
<p>mvn clean install -Dmaven.test.skip=true </p>
<p>(4)在es的plugins文件夹下创建目录ik</p>
<p>(5)将编译后生成的elasticsearch-analysis-ik-版本.zip移动到ik下，并解压</p>
<p>(6)解压后的内容移动到ik目录下</p>
<h2 id="第二节-ElasticSearch基本操作"><a href="#第二节-ElasticSearch基本操作" class="headerlink" title="第二节 ElasticSearch基本操作"></a>第二节 ElasticSearch基本操作</h2><h3 id="2-1倒排索引"><a href="#2-1倒排索引" class="headerlink" title="2.1倒排索引"></a>2.1倒排索引</h3><p>Elasticsearch 使用一种称为 倒排索引 的结构，它适用于快速的全文搜索。一个倒排索引由文档中所有不重复词的列表构成，对于其中每个词，有一个包含它的文档列表。</p>
<p>示例：</p>
<p>(1)：假设文档集合包含五个文档，每个文档内容如图所示，在图中最左端一栏是每个文档对应的文档编号。我们的任务就是对这个文档集合建立倒排索引。<br><img src="http://img.my.csdn.net/uploads/201209/10/1347267642_4728.jpg" alt="image"></p>
<p>(2):中文和英文等语言不同，单词之间没有明确分隔符号，所以首先要用分词系统将文档自动切分成单词序列。这样每个文档就转换为由单词序列构成的数据流，为了系统后续处理方便，需要对每个不同的单词赋予唯一的单词编号，同时记录下哪些文档包含这个单词，在如此处理结束后，我们可以得到最简单的倒排索引<br><img src="http://img.my.csdn.net/uploads/201202/13/0_1329141580k34Q.gif" alt="image"><br>“单词ID”一栏记录了每个单词的单词编号，第二栏是对应的单词，第三栏即每个单词对应的倒排列表</p>
<p>(3):索引系统还可以记录除此之外的更多信息,下图还记载了单词频率信息（TF）即这个单词在某个文档中的出现次数，之所以要记录这个信息，是因为词频信息在搜索结果排序时，计算查询和文档相似度是很重要的一个计算因子，所以将其记录在倒排列表中，以方便后续排序时进行分值计算。</p>
<p><img src="http://img.my.csdn.net/uploads/201209/10/1347267682_4417.jpg" alt="image"></p>
<p>(4):倒排列表中还可以记录单词在某个文档出现的位置信息</p>
<p>(1,&lt;11&gt;,1),(2,&lt;7&gt;,1),(3,&lt;3,9&gt;,2)</p>
<p>有了这个索引系统，搜索引擎可以很方便地响应用户的查询，比如用户输入查询词“Facebook”，搜索系统查找倒排索引，从中可以读出包含这个单词的文档，这些文档就是提供给用户的搜索结果，而利用单词频率信息、文档频率信息即可以对这些候选搜索结果进行排序，计算文档和查询的相似性，按照相似性得分由高到低排序输出，此即为搜索系统的部分内部流程。</p>
<h4 id="2-1-2-倒排索引原理"><a href="#2-1-2-倒排索引原理" class="headerlink" title="2.1.2 倒排索引原理"></a>2.1.2 倒排索引原理</h4><p>1.The quick brown fox jumped over the lazy dog</p>
<p>2.Quick brown foxes leap over lazy dogs in summer</p>
<p>倒排索引：</p>
<p><strong>Term      Doc_1  Doc_2</strong></p>
<p>Quick   |       |  X<br>The     |   X   |<br>brown   |   X   |  X<br>dog     |   X   |<br>dogs    |       |  X<br>fox     |   X   |<br>foxes   |       |  X<br>in      |       |  X<br>jumped  |   X   |<br>lazy    |   X   |  X<br>leap    |       |  X<br>over    |   X   |  X<br>quick   |   X   |<br>summer  |       |  X<br>the     |   X   |</p>
<p>搜索quick brown ：</p>
<p><strong>Term      Doc_1  Doc_2</strong></p>
<p>brown   |   X   |  X</p>
<p><strong>quick   |   X   |</strong></p>
<p>Total   |   2   |  1</p>
<p>计算相关度分数时，文档1的匹配度高，分数会比文档2高</p>
<p>问题：</p>
<p>Quick 和 quick 以独立的词条出现，然而用户可能认为它们是相同的词。</p>
<p>fox 和 foxes 非常相似, 就像 dog 和 dogs ；他们有相同的词根。</p>
<p>jumped 和 leap, 尽管没有相同的词根，但他们的意思很相近。他们是同义词。</p>
<p>搜索含有 Quick fox的文档是搜索不到的</p>
<p>使用标准化规则(normalization)：<br>建立倒排索引的时候，会对拆分出的各个单词进行相应的处理，以提升后面搜索的时候能够搜索到相关联的文档的概率</p>
<p><strong>Term      Doc_1  Doc_2</strong></p>
<p>brown   |   X   |  X<br>dog     |   X   |  X<br>fox     |   X   |  X<br>in      |       |  X<br>jump    |   X   |  X<br>lazy    |   X   |  X<br>over    |   X   |  X<br>quick   |   X   |  X<br>summer  |       |  X<br>the     |   X   |  X</p>
<h4 id="2-1-3-分词器介绍及内置分词器"><a href="#2-1-3-分词器介绍及内置分词器" class="headerlink" title="2.1.3 分词器介绍及内置分词器"></a>2.1.3 分词器介绍及内置分词器</h4><p>分词器：从一串文本中切分出一个一个的词条，并对每个词条进行标准化</p>
<p>包括三部分：</p>
<p>character filter：分词之前的预处理，过滤掉HTML标签，特殊符号转换等</p>
<p>tokenizer：分词</p>
<p>token filter：标准化</p>
<p>内置分词器：</p>
<p>standard 分词器：(默认的)他会将词汇单元转换成小写形式，并去除停用词和标点符号，支持中文采用的方法为单字切分</p>
<p>simple 分词器：首先会通过非字母字符来分割文本信息，然后将词汇单元统一为小写形式。该分析器会去掉数字类型的字符。</p>
<p>Whitespace 分词器：仅仅是去除空格，对字符没有lowcase化,不支持中文；<br>并且不对生成的词汇单元进行其他的标准化处理。</p>
<p>language 分词器：特定语言的分词器，不支持中文</p>
<h3 id="2-2使用ElasticSearch-API-实现CRUD"><a href="#2-2使用ElasticSearch-API-实现CRUD" class="headerlink" title="2.2使用ElasticSearch API 实现CRUD"></a>2.2使用ElasticSearch API 实现CRUD</h3><p>添加索引：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib/</span><br><span class="line">&#123;</span><br><span class="line">	<span class="attr">&quot;settings&quot;</span>:&#123;</span><br><span class="line">		<span class="attr">&quot;index&quot;</span>:&#123;</span><br><span class="line">        	<span class="attr">&quot;number_of_shards&quot;</span>: <span class="number">5</span>,</span><br><span class="line">        	<span class="attr">&quot;number_of_replicas&quot;</span>: <span class="number">1</span></span><br><span class="line">     	&#125;</span><br><span class="line">   	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>查看索引信息:</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET /lib/_settings</span><br><span class="line">GET _all/_settings</span><br></pre></td></tr></table></figure>
<p>添加文档:</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib/user/1</span><br><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;first_name&quot;</span> :  <span class="string">&quot;Jane&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;last_name&quot;</span> :   <span class="string">&quot;Smith&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;age&quot;</span> :         <span class="number">32</span>,</span><br><span class="line">    <span class="attr">&quot;about&quot;</span> :       <span class="string">&quot;I like to collect rock albums&quot;</span>,    </span><br><span class="line">    <span class="attr">&quot;interests&quot;</span>:  [ <span class="string">&quot;music&quot;</span> ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/</span><br><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;first_name&quot;</span> :  <span class="string">&quot;Douglas&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;last_name&quot;</span> :   <span class="string">&quot;Fir&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;age&quot;</span> :         <span class="number">23</span>,</span><br><span class="line">    <span class="attr">&quot;about&quot;</span>:        <span class="string">&quot;I like to build cabinets&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;interests&quot;</span>:  [ <span class="string">&quot;forestry&quot;</span> ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>查看文档:</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">GET /lib/user/1</span><br><span class="line">GET /lib/user/</span><br><span class="line">GET /lib/user/1?_source=age,interests</span><br></pre></td></tr></table></figure>

<p>更新文档:</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib/user/1</span><br><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;first_name&quot;</span> :  <span class="string">&quot;Jane&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;last_name&quot;</span> :   <span class="string">&quot;Smith&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;age&quot;</span> :         <span class="number">36</span>, </span><br><span class="line">    <span class="attr">&quot;about&quot;</span> :       <span class="string">&quot;I like to collect rock albums&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;interests&quot;</span>:  [ <span class="string">&quot;music&quot;</span> ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/1/_update</span><br><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;doc&quot;</span>:&#123;</span><br><span class="line">      <span class="attr">&quot;age&quot;</span>:<span class="number">33</span></span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>删除一个文档</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DELETE /lib/user/1</span><br></pre></td></tr></table></figure>
<p>删除一个索引</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DELETE /lib</span><br></pre></td></tr></table></figure>
<h3 id="2-3批量获取文档"><a href="#2-3批量获取文档" class="headerlink" title="2.3批量获取文档"></a>2.3批量获取文档</h3><p>使用es提供的Multi Get API：</p>
<p>使用Multi Get API可以通过索引名、类型名、文档id一次得到一个文档集合，文档可以来自同一个索引库，也可以来自不同索引库</p>
<p>使用curl命令：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">curl &#x27;http://192.168.25.131:9200/_mget&#x27; -d &#x27;&#123;</span><br><span class="line">&quot;docs&quot;：[</span><br><span class="line">   &#123;</span><br><span class="line">    <span class="attr">&quot;_index&quot;</span>: <span class="string">&quot;lib&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;_id&quot;</span>: <span class="number">1</span></span><br><span class="line">   &#125;,</span><br><span class="line">   &#123;</span><br><span class="line">     <span class="attr">&quot;_index&quot;</span>: <span class="string">&quot;lib&quot;</span>, </span><br><span class="line">     <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">     <span class="attr">&quot;_id&quot;</span>: <span class="number">2</span></span><br><span class="line">   &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;&#x27;</span><br></pre></td></tr></table></figure>
<p>在客户端工具中：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">GET /_mget</span><br><span class="line">&#123;</span><br><span class="line">	 <span class="attr">&quot;docs&quot;</span>:[</span><br><span class="line">	 	&#123;</span><br><span class="line">           <span class="attr">&quot;_index&quot;</span>: <span class="string">&quot;lib&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_id&quot;</span>: <span class="number">1</span></span><br><span class="line">       &#125;,</span><br><span class="line">       &#123;</span><br><span class="line">           <span class="attr">&quot;_index&quot;</span>: <span class="string">&quot;lib&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_id&quot;</span>: <span class="number">2</span></span><br><span class="line">       &#125;,</span><br><span class="line">       &#123;</span><br><span class="line">           <span class="attr">&quot;_index&quot;</span>: <span class="string">&quot;lib&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_id&quot;</span>: <span class="number">3</span></span><br><span class="line">       &#125;</span><br><span class="line">     ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>可以指定具体的字段：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">GET /_mget</span><br><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;docs&quot;</span>:[      </span><br><span class="line">       &#123;</span><br><span class="line">           <span class="attr">&quot;_index&quot;</span>: <span class="string">&quot;lib&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_id&quot;</span>: <span class="number">1</span>,</span><br><span class="line">           <span class="attr">&quot;_source&quot;</span>: <span class="string">&quot;interests&quot;</span></span><br><span class="line">       &#125;,</span><br><span class="line">       &#123;</span><br><span class="line">           <span class="attr">&quot;_index&quot;</span>: <span class="string">&quot;lib&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_id&quot;</span>: <span class="number">2</span>,</span><br><span class="line">           <span class="attr">&quot;_source&quot;</span>: [<span class="string">&quot;age&quot;</span>,<span class="string">&quot;interests&quot;</span>]</span><br><span class="line">       &#125; </span><br><span class="line">     ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>获取同索引同类型下的不同文档：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">GET /lib/user/_mget</span><br><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;docs&quot;</span>:[</span><br><span class="line">       &#123;</span><br><span class="line">           <span class="attr">&quot;_id&quot;</span>: <span class="number">1</span></span><br><span class="line">       &#125;,</span><br><span class="line">       &#123;</span><br><span class="line">           <span class="attr">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">           <span class="attr">&quot;_id&quot;</span>: <span class="number">2</span>,</span><br><span class="line">       &#125;</span><br><span class="line">     ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">GET /lib/user/_mget</span><br><span class="line">&#123;</span><br><span class="line">   <span class="attr">&quot;ids&quot;</span>: [<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-4使用Bulk-API-实现批量操作"><a href="#2-4使用Bulk-API-实现批量操作" class="headerlink" title="2.4使用Bulk API 实现批量操作"></a>2.4使用Bulk API 实现批量操作</h3><p>bulk的格式：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#123;action:&#123;metadata&#125;&#125;</span><br><span class="line">&#123;requstbody&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">action:(行为)</span><br><span class="line">  create：文档不存在时创建</span><br><span class="line">  update:更新文档</span><br><span class="line">  index:创建新文档或替换已有文档</span><br><span class="line">  delete:删除一个文档</span><br><span class="line">metadata：_index,_type,_id</span><br></pre></td></tr></table></figure>
<p>create 和index的区别</p>
<p>如果数据存在，使用create操作失败，会提示文档已经存在，使用index则可以成功执行。</p>
<p>示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST &#x2F;lib2</span><br><span class="line">&#123;</span><br><span class="line">	&quot;delete&quot;:&#123;</span><br><span class="line">		&quot;_index&quot;:&quot;lib&quot;,</span><br><span class="line">		&quot;_type&quot;:&quot;user&quot;,</span><br><span class="line">		&quot;_id&quot;:&quot;1&quot;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>批量添加:</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">POST /lib2/books/_bulk</span><br><span class="line">&#123;<span class="attr">&quot;index&quot;</span>:&#123;<span class="attr">&quot;_id&quot;</span>:<span class="number">1</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;title&quot;</span>:<span class="string">&quot;Java&quot;</span>,<span class="attr">&quot;price&quot;</span>:<span class="number">55</span>&#125;</span><br><span class="line"></span><br><span class="line">&#123;<span class="attr">&quot;index&quot;</span>:&#123;<span class="attr">&quot;_id&quot;</span>:<span class="number">2</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;title&quot;</span>:<span class="string">&quot;Html5&quot;</span>,<span class="attr">&quot;price&quot;</span>:<span class="number">45</span>&#125;</span><br><span class="line"></span><br><span class="line">&#123;<span class="attr">&quot;index&quot;</span>:&#123;<span class="attr">&quot;_id&quot;</span>:<span class="number">3</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;title&quot;</span>:<span class="string">&quot;Php&quot;</span>,<span class="attr">&quot;price&quot;</span>:<span class="number">35</span>&#125;</span><br><span class="line"></span><br><span class="line">&#123;<span class="attr">&quot;index&quot;</span>:&#123;<span class="attr">&quot;_id&quot;</span>:<span class="number">4</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;title&quot;</span>:<span class="string">&quot;Python&quot;</span>,<span class="attr">&quot;price&quot;</span>:<span class="number">50</span>&#125;</span><br></pre></td></tr></table></figure>
<p>批量获取:</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">GET /lib2/books/_mget</span><br><span class="line">&#123;</span><br><span class="line"><span class="attr">&quot;ids&quot;</span>: [<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;3&quot;</span>,<span class="string">&quot;4&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>删除：没有请求体</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /lib2/books/_bulk</span><br><span class="line">&#123;<span class="attr">&quot;delete&quot;</span>:&#123;<span class="attr">&quot;_index&quot;</span>:<span class="string">&quot;lib2&quot;</span>,<span class="attr">&quot;_type&quot;</span>:<span class="string">&quot;books&quot;</span>,<span class="attr">&quot;_id&quot;</span>:<span class="number">4</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;create&quot;</span>:&#123;<span class="attr">&quot;_index&quot;</span>:<span class="string">&quot;tt&quot;</span>,<span class="attr">&quot;_type&quot;</span>:<span class="string">&quot;ttt&quot;</span>,<span class="attr">&quot;_id&quot;</span>:<span class="string">&quot;100&quot;</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;name&quot;</span>:<span class="string">&quot;lisi&quot;</span>&#125;</span><br><span class="line">&#123;<span class="attr">&quot;index&quot;</span>:&#123;<span class="attr">&quot;_index&quot;</span>:<span class="string">&quot;tt&quot;</span>,<span class="attr">&quot;_type&quot;</span>:<span class="string">&quot;ttt&quot;</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;name&quot;</span>:<span class="string">&quot;zhaosi&quot;</span>&#125;</span><br><span class="line">&#123;<span class="attr">&quot;update&quot;</span>:&#123;<span class="attr">&quot;_index&quot;</span>:<span class="string">&quot;lib2&quot;</span>,<span class="attr">&quot;_type&quot;</span>:<span class="string">&quot;books&quot;</span>,<span class="attr">&quot;_id&quot;</span>:<span class="string">&quot;4&quot;</span>&#125;&#125;</span><br><span class="line">&#123;<span class="attr">&quot;doc&quot;</span>:&#123;<span class="attr">&quot;price&quot;</span>:<span class="number">58</span>&#125;&#125;</span><br></pre></td></tr></table></figure>


<p>bulk一次最大处理多少数据量:</p>
<p>　　bulk会把将要处理的数据载入内存中，所以数据量是有限制的，最佳的数据量不是一个确定的数值，它取决于你的硬件，你的文档大小以及复杂性，你的索引以及搜索的负载。</p>
<p>　　一般建议是1000-5000个文档，大小建议是5-15MB，默认不能超过100M，可以在es的配置文件（即$ES_HOME下的config下的elasticsearch.yml）中。
　　</p>
<h3 id="2-5版本控制"><a href="#2-5版本控制" class="headerlink" title="2.5版本控制"></a>2.5版本控制</h3><p>ElasticSearch采用了乐观锁来保证数据的一致性，也就是说，当用户对document进行操作时，并不需要对该document作加锁和解锁的操作，只需要指定要操作的版本即可。当版本号一致时，ElasticSearch会允许该操作顺利执行，而当版本号存在冲突时，ElasticSearch会提示冲突并抛出异常（VersionConflictEngineException异常）。</p>
<p>ElasticSearch的版本号的取值范围为1到2^63-1。</p>
<p>内部版本控制：使用的是_version</p>
<p>外部版本控制：elasticsearch在处理外部版本号时会与对内部版本号的处理有些不同。它不再是检查_version是否与请求中指定的数值<em>相同</em>,而是检查当前的_version是否比指定的数值小。如果请求成功，那么外部的版本号就会被存储到文档中的_version中。</p>
<p>为了保持_version与外部版本控制的数据一致<br>使用version_type=external</p>
<h3 id="2-6-什么是Mapping"><a href="#2-6-什么是Mapping" class="headerlink" title="2.6 什么是Mapping"></a>2.6 什么是Mapping</h3><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">PUT /myindex/article/1 </span><br><span class="line">&#123; </span><br><span class="line">  <span class="attr">&quot;post_date&quot;</span>: <span class="string">&quot;2018-05-10&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;Java&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;content&quot;</span>: <span class="string">&quot;java is the best language&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;author_id&quot;</span>: <span class="number">119</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT /myindex/article/2</span><br><span class="line">&#123; </span><br><span class="line">  <span class="attr">&quot;post_date&quot;</span>: <span class="string">&quot;2018-05-12&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;html&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;content&quot;</span>: <span class="string">&quot;I like html&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;author_id&quot;</span>: <span class="number">120</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT /myindex/article/3</span><br><span class="line">&#123; </span><br><span class="line">  <span class="attr">&quot;post_date&quot;</span>: <span class="string">&quot;2018-05-16&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;title&quot;</span>: <span class="string">&quot;es&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;content&quot;</span>: <span class="string">&quot;Es is distributed document store&quot;</span>, </span><br><span class="line">  <span class="attr">&quot;author_id&quot;</span>: <span class="number">110</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">GET /myindex/article/_search?q=2018-05</span><br><span class="line">GET /myindex/article/_search?q=2018-05-10</span><br><span class="line">GET /myindex/article/_search?q=html</span><br><span class="line">GET /myindex/article/_search?q=java</span><br></pre></td></tr></table></figure>


<p>#查看es自动创建的mapping</p>
<p>GET /myindex/article/_mapping</p>
<p>es自动创建了index，type，以及type对应的mapping(dynamic mapping)</p>
<p>什么是映射：mapping定义了type中的每个字段的数据类型以及这些字段如何分词等相关属性</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;myindex&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;mappings&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">&quot;article&quot;</span>: &#123;</span><br><span class="line">        <span class="attr">&quot;properties&quot;</span>: &#123;</span><br><span class="line">          <span class="attr">&quot;author_id&quot;</span>: &#123;</span><br><span class="line">            <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;long&quot;</span></span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">&quot;content&quot;</span>: &#123;</span><br><span class="line">            <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;fields&quot;</span>: &#123;</span><br><span class="line">              <span class="attr">&quot;keyword&quot;</span>: &#123;</span><br><span class="line">                <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;keyword&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;ignore_above&quot;</span>: <span class="number">256</span></span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">&quot;post_date&quot;</span>: &#123;</span><br><span class="line">            <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;date&quot;</span></span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">&quot;title&quot;</span>: &#123;</span><br><span class="line">            <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>,</span><br><span class="line">            <span class="attr">&quot;fields&quot;</span>: &#123;</span><br><span class="line">              <span class="attr">&quot;keyword&quot;</span>: &#123;</span><br><span class="line">                <span class="attr">&quot;type&quot;</span>: <span class="string">&quot;keyword&quot;</span>,</span><br><span class="line">                <span class="attr">&quot;ignore_above&quot;</span>: <span class="number">256</span></span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>创建索引的时候,可以预先定义字段的类型以及相关属性，这样就能够把日期字段处理成日期，把数字字段处理成数字，把字符串字段处理字符串值等</p>
<p>支持的数据类型：</p>
<p>(1)核心数据类型（Core datatypes）</p>
<pre><code>字符型：string，string类型包括
text 和 keyword

text类型被用来索引长文本，在建立索引前会将这些文本进行分词，转化为词的组合，建立索引。允许es来检索这些词语。text类型不能用来排序和聚合。

Keyword类型不需要进行分词，可以被用来检索过滤、排序和聚合。keyword 类型字段只能用本身来进行检索

数字型：long, integer, short, byte, double, float
日期型：date
布尔型：boolean
二进制型：binary
</code></pre>
<p>​<br>​<br>​    </p>
<p>(2)复杂数据类型（Complex datatypes）</p>
<pre><code>数组类型（Array datatype）：数组类型不需要专门指定数组元素的type，例如：
    字符型数组: [ &quot;one&quot;, &quot;two&quot; ]
    整型数组：[ 1, 2 ]
    数组型数组：[ 1, [ 2, 3 ]] 等价于[ 1, 2, 3 ]
    对象数组：[ &#123; &quot;name&quot;: &quot;Mary&quot;, &quot;age&quot;: 12 &#125;, &#123; &quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 10 &#125;]
对象类型（Object datatype）：_ object _ 用于单个JSON对象；
嵌套类型（Nested datatype）：_ nested _ 用于JSON数组；
</code></pre>
<p>(3)地理位置类型（Geo datatypes）</p>
<pre><code>地理坐标类型（Geo-point datatype）：_ geo_point _ 用于经纬度坐标；
地理形状类型（Geo-Shape datatype）：_ geo_shape _ 用于类似于多边形的复杂形状；
</code></pre>
<p>(4)特定类型（Specialised datatypes）</p>
<pre><code>IPv4 类型（IPv4 datatype）：_ ip _ 用于IPv4 地址；
Completion 类型（Completion datatype）：_ completion _提供自动补全建议；
Token count 类型（Token count datatype）：_ token_count _ 用于统计做了标记的字段的index数目，该值会一直增加，不会因为过滤条件而减少。
mapper-murmur3
类型：通过插件，可以通过 _ murmur3 _ 来计算 index 的 hash 值；
附加类型（Attachment datatype）：采用 mapper-attachments
插件，可支持_ attachments _ 索引，例如 Microsoft Office 格式，Open Document 格式，ePub, HTML 等。
</code></pre>
<p>支持的属性：</p>
<p>“store”:false//是否单独设置此字段的是否存储而从_source字段中分离，默认是false，只能搜索，不能获取值</p>
<p>“index”: true//分词，不分词是：false<br>   ，设置成false，字段将不会被索引</p>
<p>“analyzer”:”ik”//指定分词器,默认分词器为standard analyzer</p>
<p>“boost”:1.23//字段级别的分数加权，默认值是1.0</p>
<p>“doc_values”:false//对not_analyzed字段，默认都是开启，分词字段不能使用，对排序和聚合能提升较大性能，节约内存</p>
<p>“fielddata”:{“format”:”disabled”}//针对分词字段，参与排序或聚合时能提高性能，不分词字段统一建议使用doc_value</p>
<p>“fields”:{“raw”:{“type”:”string”,”index”:”not_analyzed”}} //可以对一个字段提供多种索引模式，同一个字段的值，一个分词，一个不分词</p>
<p>“ignore_above”:100 //超过100个字符的文本，将会被忽略，不被索引</p>
<p>“include_in_all”:ture//设置是否此字段包含在_all字段中，默认是true，除非index设置成no选项</p>
<p>“index_options”:”docs”//4个可选参数docs（索引文档号） ,freqs（文档号+词频），positions（文档号+词频+位置，通常用来距离查询），offsets（文档号+词频+位置+偏移量，通常被使用在高亮字段）分词字段默认是position，其他的默认是docs</p>
<p>“norms”:{“enable”:true,”loading”:”lazy”}//分词字段默认配置，不分词字段：默认{“enable”:false}，存储长度因子和索引时boost，建议对需要参与评分字段使用 ，会额外增加内存消耗量</p>
<p>“null_value”:”NULL”//设置一些缺失字段的初始化值，只有string可以使用，分词字段的null值也会被分词</p>
<p>“position_increament_gap”:0//影响距离查询或近似查询，可以设置在多值字段的数据上火分词字段上，查询时可指定slop间隔，默认值是100</p>
<p>“search_analyzer”:”ik”//设置搜索时的分词器，默认跟ananlyzer是一致的，比如index时用standard+ngram，搜索时用standard用来完成自动提示功能</p>
<p>“similarity”:”BM25”//默认是TF/IDF算法，指定一个字段评分策略，仅仅对字符串型和分词类型有效</p>
<p>“term_vector”:”no”//默认不存储向量信息，支持参数yes（term存储），with_positions（term+位置）,with_offsets（term+偏移量），with_positions_offsets(term+位置+偏移量) 对快速高亮fast vector highlighter能提升性能，但开启又会加大索引体积，不适合大数据量用</p>
<p>映射的分类：</p>
<p>(1)动态映射：</p>
<p>当ES在文档中碰到一个以前没见过的字段时，它会利用动态映射来决定该字段的类型，并自动地对该字段添加映射。</p>
<p>可以通过dynamic设置来控制这一行为，它能够接受以下的选项：</p>
<pre><code>true：默认值。动态添加字段
false：忽略新字段
strict：如果碰到陌生字段，抛出异常
</code></pre>
<p>dynamic设置可以适用在根对象上或者object类型的任意字段上。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">POST /lib2</span><br><span class="line"># 给索引lib2创建映射类型</span><br><span class="line">&#123;</span><br><span class="line"><span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line"><span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line"><span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span></span><br><span class="line">&#125;,</span><br><span class="line"> <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">  <span class="string">&quot;books&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;properties&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;title&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">        <span class="string">&quot;name&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>,<span class="string">&quot;index&quot;</span>:<span class="keyword">false</span>&#125;,</span><br><span class="line">        <span class="string">&quot;publish_date&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;date&quot;</span>,<span class="string">&quot;index&quot;</span>:<span class="keyword">false</span>&#125;,</span><br><span class="line">        <span class="string">&quot;price&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;double&quot;</span>&#125;,</span><br><span class="line">        <span class="string">&quot;number&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;integer&quot;</span>&#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">POST /lib2</span><br><span class="line"># 给索引lib2创建映射类型</span><br><span class="line">&#123;</span><br><span class="line"><span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line"><span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line"><span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span></span><br><span class="line">&#125;,</span><br><span class="line"> <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">  <span class="string">&quot;books&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;properties&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;title&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">        <span class="string">&quot;name&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>,<span class="string">&quot;index&quot;</span>:<span class="keyword">false</span>&#125;,</span><br><span class="line">        <span class="string">&quot;publish_date&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;date&quot;</span>,<span class="string">&quot;index&quot;</span>:<span class="keyword">false</span>&#125;,</span><br><span class="line">        <span class="string">&quot;price&quot;</span>:&#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;double&quot;</span>&#125;,</span><br><span class="line">        <span class="string">&quot;number&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;type&quot;</span>:<span class="string">&quot;object&quot;</span>,</span><br><span class="line">            <span class="string">&quot;dynamic&quot;</span>:<span class="keyword">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-7基本查询-Query查询"><a href="#2-7基本查询-Query查询" class="headerlink" title="2.7基本查询(Query查询)"></a>2.7基本查询(Query查询)</h3><h4 id="2-7-1数据准备"><a href="#2-7-1数据准备" class="headerlink" title="2.7.1数据准备"></a>2.7.1数据准备</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib3</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">     <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;user&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;properties&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;address&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;integer&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;birthday&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;date&quot;</span>&#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search?q=name:lisi</span><br><span class="line">GET /lib3/user/_search?q=name:zhaoliu&amp;sort=age:desc</span><br></pre></td></tr></table></figure>


<h4 id="2-7-2-term查询和terms查询"><a href="#2-7-2-term查询和terms查询" class="headerlink" title="2.7.2 term查询和terms查询"></a>2.7.2 term查询和terms查询</h4><p>term query会去倒排索引中寻找确切的term，它并不知道分词器的存在。这种查询适合keyword 、numeric、date。</p>
<p>term:查询某个字段里含有某个关键词的文档</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search/</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;term&quot;</span>: &#123;<span class="string">&quot;interests&quot;</span>: <span class="string">&quot;changge&quot;</span>&#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>terms:查询某个字段里含有多个关键词的文档</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;terms&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: [<span class="string">&quot;hejiu&quot;</span>,<span class="string">&quot;changge&quot;</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-7-3-控制查询返回的数量"><a href="#2-7-3-控制查询返回的数量" class="headerlink" title="2.7.3 控制查询返回的数量"></a>2.7.3 控制查询返回的数量</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">from：从哪一个文档开始</span><br><span class="line">size：需要的个数</span><br><span class="line"></span><br><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;from&quot;</span>:<span class="number">0</span>,</span><br><span class="line">    <span class="string">&quot;size&quot;</span>:<span class="number">2</span>,</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;terms&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: [<span class="string">&quot;hejiu&quot;</span>,<span class="string">&quot;changge&quot;</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-7-4-返回版本号"><a href="#2-7-4-返回版本号" class="headerlink" title="2.7.4 返回版本号"></a>2.7.4 返回版本号</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;version&quot;</span>:<span class="keyword">true</span>,</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;terms&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: [<span class="string">&quot;hejiu&quot;</span>,<span class="string">&quot;changge&quot;</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-7-5-match查询"><a href="#2-7-5-match查询" class="headerlink" title="2.7.5 match查询"></a>2.7.5 match查询</h4><p>match query知道<strong>分词器</strong>的存在，会对filed进行分词操作，然后再查询</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: <span class="string">&quot;zhaoliu&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">20</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>match_all:查询所有文档</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>multi_match:可以指定多个字段</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;multi_match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;query&quot;</span>: <span class="string">&quot;lvyou&quot;</span>,</span><br><span class="line">            <span class="string">&quot;fields&quot;</span>: [<span class="string">&quot;interests&quot;</span>,<span class="string">&quot;name&quot;</span>]</span><br><span class="line">         &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>match_phrase:短语匹配查询</p>
<p>ElasticSearch引擎首先分析（analyze）查询字符串，从分析后的文本中构建短语查询，这意味着必须匹配短语中的所有分词，并且保证各个分词的相对位置不变：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>:&#123;  </span><br><span class="line">      <span class="string">&quot;match_phrase&quot;</span>:&#123;  </span><br><span class="line">         <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;duanlian，shuoxiangsheng&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="2-7-6-指定返回的字段"><a href="#2-7-6-指定返回的字段" class="headerlink" title="2.7.6 指定返回的字段"></a>2.7.6 指定返回的字段</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">0GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;_source&quot;</span>: [<span class="string">&quot;address&quot;</span>,<span class="string">&quot;name&quot;</span>],</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;changge&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="2-7-7控制加载的字段"><a href="#2-7-7控制加载的字段" class="headerlink" title="2.7.7控制加载的字段"></a>2.7.7控制加载的字段</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;_source&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;includes&quot;</span>: [<span class="string">&quot;name&quot;</span>,<span class="string">&quot;address&quot;</span>],</span><br><span class="line">          <span class="string">&quot;excludes&quot;</span>: [<span class="string">&quot;age&quot;</span>,<span class="string">&quot;birthday&quot;</span>]</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用通配符*</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;_source&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;includes&quot;</span>: <span class="string">&quot;addr*&quot;</span>,</span><br><span class="line">          <span class="string">&quot;excludes&quot;</span>: [<span class="string">&quot;name&quot;</span>,<span class="string">&quot;bir*&quot;</span>]</span><br><span class="line">		&#125;,</span><br><span class="line">	<span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    	<span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="2-7-8-排序"><a href="#2-7-8-排序" class="headerlink" title="2.7.8 排序"></a>2.7.8 排序</h4><p>使用sort实现排序：<br>desc:降序，asc升序</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;sort&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">           <span class="string">&quot;age&quot;</span>: &#123;</span><br><span class="line">               <span class="string">&quot;order&quot;</span>:<span class="string">&quot;asc&quot;</span></span><br><span class="line">           &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]     </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">GET &#x2F;lib3&#x2F;user&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;: &#123;</span><br><span class="line">        &quot;match_all&quot;: &#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;sort&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">           &quot;age&quot;: &#123;</span><br><span class="line">               &quot;order&quot;:&quot;desc&quot;</span><br><span class="line">           &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]       </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="2-7-9-前缀匹配查询"><a href="#2-7-9-前缀匹配查询" class="headerlink" title="2.7.9 前缀匹配查询"></a>2.7.9 前缀匹配查询</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match_phrase_prefix&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;name&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;query&quot;</span>: <span class="string">&quot;zhao&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-7-10-范围查询"><a href="#2-7-10-范围查询" class="headerlink" title="2.7.10 范围查询"></a>2.7.10 范围查询</h4><p>range:实现范围查询</p>
<p>参数：from,to,include_lower,include_upper,boost</p>
<p>include_lower:是否包含范围的左边界，默认是true</p>
<p>include_upper:是否包含范围的右边界，默认是true</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;range&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;birthday&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;from&quot;</span>: <span class="string">&quot;1990-10-10&quot;</span>,</span><br><span class="line">                <span class="string">&quot;to&quot;</span>: <span class="string">&quot;2018-05-01&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;range&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;from&quot;</span>: <span class="number">20</span>,</span><br><span class="line">                <span class="string">&quot;to&quot;</span>: <span class="number">25</span>,</span><br><span class="line">                <span class="string">&quot;include_lower&quot;</span>: <span class="keyword">true</span>,</span><br><span class="line">                <span class="string">&quot;include_upper&quot;</span>: <span class="keyword">false</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-7-11-wildcard查询"><a href="#2-7-11-wildcard查询" class="headerlink" title="2.7.11 wildcard查询"></a>2.7.11 wildcard查询</h4><p>允许使用通配符* 和 ?来进行查询</p>
<ul>
<li><p>*代表0个或多个字符</p>
</li>
<li><p>？代表任意一个字符</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;wildcard&quot;</span>: &#123;</span><br><span class="line">             <span class="string">&quot;name&quot;</span>: <span class="string">&quot;zhao*&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;wildcard&quot;</span>: &#123;</span><br><span class="line">             <span class="string">&quot;name&quot;</span>: <span class="string">&quot;li?i&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="2-7-12-fuzzy实现模糊查询"><a href="#2-7-12-fuzzy实现模糊查询" class="headerlink" title="2.7.12 fuzzy实现模糊查询"></a>2.7.12 fuzzy实现模糊查询</h4><ul>
<li><p>value：查询的关键字</p>
</li>
<li><p>boost：查询的权值，默认值是1.0</p>
</li>
<li><p>min_similarity:设置匹配的最小相似度，默认值为0.5，对于字符串，取值为0-1(包括0和1);对于数值，取值可能大于1;对于日期型取值为1d,1m等，1d就代表1天</p>
</li>
<li><p>prefix_length:指明区分词项的共同前缀长度，默认是0</p>
</li>
<li><p>max_expansions:查询中的词项可以扩展的数目，默认可以无限大</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;fuzzy&quot;</span>: &#123;</span><br><span class="line">             <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;chagge&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GET &#x2F;lib3&#x2F;user&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;: &#123;</span><br><span class="line">        &quot;fuzzy&quot;: &#123;</span><br><span class="line">             &quot;interests&quot;: &#123;</span><br><span class="line">                 &quot;value&quot;: &quot;chagge&quot;</span><br><span class="line">             &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-7-13-高亮搜索结果"><a href="#2-7-13-高亮搜索结果" class="headerlink" title="2.7.13 高亮搜索结果"></a>2.7.13 高亮搜索结果</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;changge&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;highlight&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;fields&quot;</span>: &#123;</span><br><span class="line">             <span class="string">&quot;interests&quot;</span>: &#123;&#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-8-Filter查询"><a href="#2-8-Filter查询" class="headerlink" title="2.8 Filter查询"></a>2.8 Filter查询</h3><p>filter是<strong>不计算相关性</strong>的，同时可以cache。因此，<em>filter速度要快于query</em>。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST /lib4/items/_bulk</span><br><span class="line">&#123;<span class="string">&quot;index&quot;</span>: &#123;<span class="string">&quot;_id&quot;</span>: <span class="number">1</span>&#125;&#125;</span><br><span class="line">&#123;<span class="string">&quot;price&quot;</span>: <span class="number">40</span>,<span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;ID100123&quot;</span>&#125;</span><br><span class="line">&#123;<span class="string">&quot;index&quot;</span>: &#123;<span class="string">&quot;_id&quot;</span>: <span class="number">2</span>&#125;&#125;</span><br><span class="line">&#123;<span class="string">&quot;price&quot;</span>: <span class="number">50</span>,<span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;ID100124&quot;</span>&#125;</span><br><span class="line">&#123;<span class="string">&quot;index&quot;</span>: &#123;<span class="string">&quot;_id&quot;</span>: <span class="number">3</span>&#125;&#125;</span><br><span class="line">&#123;<span class="string">&quot;price&quot;</span>: <span class="number">25</span>,<span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;ID100124&quot;</span>&#125;</span><br><span class="line">&#123;<span class="string">&quot;index&quot;</span>: &#123;<span class="string">&quot;_id&quot;</span>: <span class="number">4</span>&#125;&#125;</span><br><span class="line">&#123;<span class="string">&quot;price&quot;</span>: <span class="number">30</span>,<span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;ID100125&quot;</span>&#125;</span><br><span class="line">&#123;<span class="string">&quot;index&quot;</span>: &#123;<span class="string">&quot;_id&quot;</span>: <span class="number">5</span>&#125;&#125;</span><br><span class="line">&#123;<span class="string">&quot;price&quot;</span>: <span class="keyword">null</span>,<span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;ID100127&quot;</span>&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-8-1-简单的过滤查询"><a href="#2-8-1-简单的过滤查询" class="headerlink" title="2.8.1 简单的过滤查询"></a>2.8.1 简单的过滤查询</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123; </span><br><span class="line">       <span class="string">&quot;post_filter&quot;</span>: &#123;</span><br><span class="line">             <span class="string">&quot;term&quot;</span>: &#123;</span><br><span class="line">                 <span class="string">&quot;price&quot;</span>: <span class="number">40</span></span><br><span class="line">             &#125;</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">      <span class="string">&quot;post_filter&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;terms&quot;</span>: &#123;</span><br><span class="line">                 <span class="string">&quot;price&quot;</span>: [<span class="number">25</span>,<span class="number">40</span>]</span><br><span class="line">              &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;post_filter&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;term&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;ID100123&quot;</span></span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>查看分词器分析的结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/_mapping</span><br></pre></td></tr></table></figure>

<p>不希望商品id字段被分词，则重新创建映射</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">DELETE lib4</span><br><span class="line"></span><br><span class="line">PUT /lib4</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;mappings&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;items&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;properties&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;itemID&quot;</span>: &#123;</span><br><span class="line">                    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>,</span><br><span class="line">                    <span class="string">&quot;index&quot;</span>: <span class="keyword">false</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="2-8-2-bool过滤查询"><a href="#2-8-2-bool过滤查询" class="headerlink" title="2.8.2 bool过滤查询"></a>2.8.2 bool过滤查询</h4><p>可以实现组合过滤查询</p>
<p>格式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;bool&quot;: &#123;</span><br><span class="line">        &quot;must&quot;: [],</span><br><span class="line">        &quot;should&quot;: [],</span><br><span class="line">        &quot;must_not&quot;: []</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>must:必须满足的条件—and</p>
</li>
<li><p>should：可以满足也可以不满足的条件–or</p>
</li>
<li><p>must_not:不需要满足的条件–not</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">GET &#x2F;lib4&#x2F;items&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">    &quot;post_filter&quot;: &#123;</span><br><span class="line">          &quot;bool&quot;: &#123;</span><br><span class="line">               &quot;should&quot;: [</span><br><span class="line">                    &#123;&quot;term&quot;: &#123;&quot;price&quot;:25&#125;&#125;,</span><br><span class="line">                    &#123;&quot;term&quot;: &#123;&quot;itemID&quot;: &quot;id100123&quot;&#125;&#125;</span><br><span class="line">              ],</span><br><span class="line">            &quot;must_not&quot;: &#123;</span><br><span class="line">                &quot;term&quot;:&#123;&quot;price&quot;: 30&#125;</span><br><span class="line">               &#125;</span><br><span class="line">            &#125;</span><br><span class="line">         &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>嵌套使用bool：    </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;post_filter&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;should&quot;</span>: [</span><br><span class="line">                    &#123;<span class="string">&quot;term&quot;</span>: &#123;<span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;id100123&quot;</span>&#125;&#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                      <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">                          <span class="string">&quot;must&quot;</span>: [</span><br><span class="line">                              &#123;<span class="string">&quot;term&quot;</span>: &#123;<span class="string">&quot;itemID&quot;</span>: <span class="string">&quot;id100124&quot;</span>&#125;&#125;,</span><br><span class="line">                              &#123;<span class="string">&quot;term&quot;</span>: &#123;<span class="string">&quot;price&quot;</span>: <span class="number">40</span>&#125;&#125;</span><br><span class="line">                            ]</span><br><span class="line">                          &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                  ]</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-8-3-范围过滤"><a href="#2-8-3-范围过滤" class="headerlink" title="2.8.3 范围过滤"></a>2.8.3 范围过滤</h4><p>使用range:</p>
<ul>
<li><p>gt:  &gt;</p>
</li>
<li><p>lt:  &lt;</p>
</li>
<li><p>gte:  &gt;=</p>
</li>
<li><p>lte:   &lt;=</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">     <span class="string">&quot;post_filter&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;range&quot;</span>: &#123;</span><br><span class="line">              <span class="string">&quot;price&quot;</span>: &#123;</span><br><span class="line">                   <span class="string">&quot;gt&quot;</span>: <span class="number">25</span>,</span><br><span class="line">                   <span class="string">&quot;lt&quot;</span>: <span class="number">50</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="2-8-5-过滤非空"><a href="#2-8-5-过滤非空" class="headerlink" title="2.8.5 过滤非空"></a>2.8.5 过滤非空</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;filter&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;exists&quot;</span>:&#123;</span><br><span class="line">             <span class="string">&quot;field&quot;</span>:<span class="string">&quot;price&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span> : &#123;</span><br><span class="line">        <span class="string">&quot;constant_score&quot;</span> : &#123;</span><br><span class="line">            <span class="string">&quot;filter&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;exists&quot;</span> : &#123; <span class="string">&quot;field&quot;</span> : <span class="string">&quot;price&quot;</span> &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="2-8-6-过滤器缓存"><a href="#2-8-6-过滤器缓存" class="headerlink" title="2.8.6 过滤器缓存"></a>2.8.6 过滤器缓存</h4><p>ElasticSearch提供了一种特殊的缓存，即过滤器缓存（filter cache），用来存储过滤器的结果，被缓存的过滤器并不需要消耗过多的内存（因为它们只存储了哪些文档能与过滤器相匹配的相关信息），而且可供后续所有与之相关的查询重复使用，从而极大地提高了查询性能。</p>
<p>注意：ElasticSearch并不是默认缓存所有过滤器，<br>以下过滤器默认不缓存：</p>
<pre><code>numeric_range
script
geo_bbox
geo_distance
geo_distance_range
geo_polygon
geo_shape
and
or
not
</code></pre>
<p>exists,missing,range,term,terms默认是开启缓存的</p>
<p>开启方式：在filter查询语句后边加上<br><em>“_catch”:true</em></p>
<h3 id="2-9-聚合查询"><a href="#2-9-聚合查询" class="headerlink" title="2.9 聚合查询"></a>2.9 聚合查询</h3><p>(1)sum</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;size&quot;</span>:<span class="number">0</span>,</span><br><span class="line">  <span class="string">&quot;aggs&quot;</span>: &#123;</span><br><span class="line">     <span class="string">&quot;price_of_sum&quot;</span>: &#123;</span><br><span class="line">         <span class="string">&quot;sum&quot;</span>: &#123;</span><br><span class="line">           <span class="string">&quot;field&quot;</span>: <span class="string">&quot;price&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>(2)min</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;size&quot;</span>: <span class="number">0</span>, </span><br><span class="line">  <span class="string">&quot;aggs&quot;</span>: &#123;</span><br><span class="line">     <span class="string">&quot;price_of_min&quot;</span>: &#123;</span><br><span class="line">         <span class="string">&quot;min&quot;</span>: &#123;</span><br><span class="line">           <span class="string">&quot;field&quot;</span>: <span class="string">&quot;price&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>(3)max</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;size&quot;</span>: <span class="number">0</span>, </span><br><span class="line">  <span class="string">&quot;aggs&quot;</span>: &#123;</span><br><span class="line">     <span class="string">&quot;price_of_max&quot;</span>: &#123;</span><br><span class="line">         <span class="string">&quot;max&quot;</span>: &#123;</span><br><span class="line">           <span class="string">&quot;field&quot;</span>: <span class="string">&quot;price&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>(4)avg</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;size&quot;</span>:<span class="number">0</span>,</span><br><span class="line">  <span class="string">&quot;aggs&quot;</span>: &#123;</span><br><span class="line">     <span class="string">&quot;price_of_avg&quot;</span>: &#123;</span><br><span class="line">         <span class="string">&quot;avg&quot;</span>: &#123;</span><br><span class="line">           <span class="string">&quot;field&quot;</span>: <span class="string">&quot;price&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>(5)cardinality:求基数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;size&quot;</span>:<span class="number">0</span>,</span><br><span class="line">  <span class="string">&quot;aggs&quot;</span>: &#123;</span><br><span class="line">     <span class="string">&quot;price_of_cardi&quot;</span>: &#123;</span><br><span class="line">         <span class="string">&quot;cardinality&quot;</span>: &#123;</span><br><span class="line">           <span class="string">&quot;field&quot;</span>: <span class="string">&quot;price&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>(6)terms:分组</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET /lib4/items/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;size&quot;</span>:<span class="number">0</span>,</span><br><span class="line">  <span class="string">&quot;aggs&quot;</span>: &#123;</span><br><span class="line">     <span class="string">&quot;price_group_by&quot;</span>: &#123;</span><br><span class="line">         <span class="string">&quot;terms&quot;</span>: &#123;</span><br><span class="line">           <span class="string">&quot;field&quot;</span>: <span class="string">&quot;price&quot;</span></span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>对那些有唱歌兴趣的用户按年龄分组</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;changge&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">   &#125;,</span><br><span class="line">   <span class="string">&quot;size&quot;</span>: <span class="number">0</span>, </span><br><span class="line">   <span class="string">&quot;aggs&quot;</span>:&#123;</span><br><span class="line">       <span class="string">&quot;age_group_by&quot;</span>:&#123;</span><br><span class="line">           <span class="string">&quot;terms&quot;</span>: &#123;</span><br><span class="line">             <span class="string">&quot;field&quot;</span>: <span class="string">&quot;age&quot;</span>,</span><br><span class="line">             <span class="string">&quot;order&quot;</span>: &#123;</span><br><span class="line">               <span class="string">&quot;avg_of_age&quot;</span>: <span class="string">&quot;desc&quot;</span></span><br><span class="line">             &#125;</span><br><span class="line">           &#125;,</span><br><span class="line">           <span class="string">&quot;aggs&quot;</span>: &#123;</span><br><span class="line">             <span class="string">&quot;avg_of_age&quot;</span>: &#123;</span><br><span class="line">               <span class="string">&quot;avg&quot;</span>: &#123;</span><br><span class="line">                 <span class="string">&quot;field&quot;</span>: <span class="string">&quot;age&quot;</span></span><br><span class="line">               &#125;</span><br><span class="line">             &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-10-复合查询"><a href="#2-10-复合查询" class="headerlink" title="2.10 复合查询"></a>2.10 复合查询</h3><p>将多个基本查询组合成单一查询的查询</p>
<h4 id="2-10-1-使用bool查询"><a href="#2-10-1-使用bool查询" class="headerlink" title="2.10.1 使用bool查询"></a>2.10.1 使用bool查询</h4><p>接收以下参数：</p>
<ul>
<li>must：文档 必须匹配这些条件才能被包含进来。 </li>
<li>must_not： 文档 必须不匹配这些条件才能被包含进来。 </li>
<li>should：如果满足这些语句中的任意语句，将增加 _score，否则，无任何影响。它们主要用于修正每个文档的相关性得分。   </li>
<li>filter：必须 匹配，但它以不评分、过滤模式来进行。这些语句对评分没有贡献，只是根据过滤标准来排除或包含文档。</li>
</ul>
<p>相关性得分是如何组合的。每一个子查询都独自地计算文档的相关性得分。一旦他们的得分被计算出来， bool 查询就将这些得分进行合并并且返回一个代表整个布尔操作的得分。</p>
<p>下面的查询用于查找 title 字段匹配 how to make millions 并且不被标识为 spam 的文档。那些被标识为 starred 或在2014之后的文档，将比另外那些文档拥有更高的排名。如果 <em>两者</em> 都满足，那么它排名将更高：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;bool&quot;: &#123;</span><br><span class="line">        &quot;must&quot;: &#123; &quot;match&quot;: &#123; &quot;title&quot;: &quot;how to make millions&quot; &#125;&#125;,</span><br><span class="line">        &quot;must_not&quot;: &#123; &quot;match&quot;: &#123; &quot;tag&quot;:   &quot;spam&quot; &#125;&#125;,</span><br><span class="line">        &quot;should&quot;: [</span><br><span class="line">            &#123; &quot;match&quot;: &#123; &quot;tag&quot;: &quot;starred&quot; &#125;&#125;,</span><br><span class="line">            &#123; &quot;range&quot;: &#123; &quot;date&quot;: &#123; &quot;gte&quot;: &quot;2014-01-01&quot; &#125;&#125;&#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>如果没有 must 语句，那么至少需要能够匹配其中的一条 should 语句。但，如果存在至少一条 must 语句，则对 should 语句的匹配没有要求。<br>如果我们不想因为文档的时间而影响得分，可以用 filter 语句来重写前面的例子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;must&quot;</span>: &#123; <span class="string">&quot;match&quot;</span>: &#123; <span class="string">&quot;title&quot;</span>: <span class="string">&quot;how to make millions&quot;</span> &#125;&#125;,</span><br><span class="line">        <span class="string">&quot;must_not&quot;</span>: &#123; <span class="string">&quot;match&quot;</span>: &#123; <span class="string">&quot;tag&quot;</span>:   <span class="string">&quot;spam&quot;</span> &#125;&#125;,</span><br><span class="line">        <span class="string">&quot;should&quot;</span>: [</span><br><span class="line">            &#123; <span class="string">&quot;match&quot;</span>: &#123; <span class="string">&quot;tag&quot;</span>: <span class="string">&quot;starred&quot;</span> &#125;&#125;</span><br><span class="line">        ],</span><br><span class="line">        <span class="string">&quot;filter&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;range&quot;</span>: &#123; <span class="string">&quot;date&quot;</span>: &#123; <span class="string">&quot;gte&quot;</span>: <span class="string">&quot;2014-01-01&quot;</span> &#125;&#125; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>通过将 range 查询移到 filter 语句中，我们将它转成不评分的查询，将不再影响文档的相关性排名。由于它现在是一个不评分的查询，可以使用各种对 filter 查询有效的优化手段来提升性能。</p>
<p>bool 查询本身也可以被用做不评分的查询。简单地将它放置到 filter 语句中并在内部构建布尔逻辑：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;must&quot;</span>: &#123; <span class="string">&quot;match&quot;</span>: &#123; <span class="string">&quot;title&quot;</span>: <span class="string">&quot;how to make millions&quot;</span> &#125;&#125;,</span><br><span class="line">        <span class="string">&quot;must_not&quot;</span>: &#123; <span class="string">&quot;match&quot;</span>: &#123; <span class="string">&quot;tag&quot;</span>:   <span class="string">&quot;spam&quot;</span> &#125;&#125;,</span><br><span class="line">        <span class="string">&quot;should&quot;</span>: [</span><br><span class="line">            &#123; <span class="string">&quot;match&quot;</span>: &#123; <span class="string">&quot;tag&quot;</span>: <span class="string">&quot;starred&quot;</span> &#125;&#125;</span><br><span class="line">        ],</span><br><span class="line">        <span class="string">&quot;filter&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;bool&quot;</span>: &#123; </span><br><span class="line">              <span class="string">&quot;must&quot;</span>: [</span><br><span class="line">                  &#123; <span class="string">&quot;range&quot;</span>: &#123; <span class="string">&quot;date&quot;</span>: &#123; <span class="string">&quot;gte&quot;</span>: <span class="string">&quot;2014-01-01&quot;</span> &#125;&#125;&#125;,</span><br><span class="line">                  &#123; <span class="string">&quot;range&quot;</span>: &#123; <span class="string">&quot;price&quot;</span>: &#123; <span class="string">&quot;lte&quot;</span>: <span class="number">29.99</span> &#125;&#125;&#125;</span><br><span class="line">              ],</span><br><span class="line">              <span class="string">&quot;must_not&quot;</span>: [</span><br><span class="line">                  &#123; <span class="string">&quot;term&quot;</span>: &#123; <span class="string">&quot;category&quot;</span>: <span class="string">&quot;ebooks&quot;</span> &#125;&#125;</span><br><span class="line">              ]</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="2-10-2-constant-score查询"><a href="#2-10-2-constant-score查询" class="headerlink" title="2.10.2 constant_score查询"></a>2.10.2 constant_score查询</h4><p>它将一个不变的常量评分应用于所有匹配的文档。它被经常用于你只需要执行一个 filter 而没有其它查询（例如，评分查询）的情况下。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;constant_score&quot;</span>:   &#123;</span><br><span class="line">        <span class="string">&quot;filter&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;term&quot;</span>: &#123; <span class="string">&quot;category&quot;</span>: <span class="string">&quot;ebooks&quot;</span> &#125; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>term 查询被放置在 constant_score 中，转成不评分的filter。这种方式可以用来取代只有 filter 语句的 bool 查询。 </p>
<h2 id="第三节-ElasticSearch原理"><a href="#第三节-ElasticSearch原理" class="headerlink" title="第三节 ElasticSearch原理"></a>第三节 ElasticSearch原理</h2><h3 id="3-1-解析es的分布式架构"><a href="#3-1-解析es的分布式架构" class="headerlink" title="3.1 解析es的分布式架构"></a>3.1 解析es的分布式架构</h3><h4 id="3-1-1-分布式架构的透明隐藏特性"><a href="#3-1-1-分布式架构的透明隐藏特性" class="headerlink" title="3.1.1 分布式架构的透明隐藏特性"></a>3.1.1 分布式架构的透明隐藏特性</h4><p>ElasticSearch是一个分布式系统，隐藏了复杂的处理机制</p>
<ul>
<li><p>分片机制：我们不用关心数据是按照什么机制分片的、最后放入到哪个分片中</p>
</li>
<li><p>分片的副本：</p>
</li>
<li><p>集群发现机制(cluster discovery)：比如当前我们启动了一个es进程，当启动了第二个es进程时，这个进程作为一个node自动就发现了集群，并且加入了进去</p>
</li>
<li><p>shard负载均衡：比如现在有10shard，集群中有3个节点，es会进行均衡的进行分配，以保持每个节点均衡的负载请求 </p>
</li>
<li><p>请求路由</p>
</li>
</ul>
<h4 id="3-1-2-扩容机制"><a href="#3-1-2-扩容机制" class="headerlink" title="3.1.2 扩容机制"></a>3.1.2 扩容机制</h4><ul>
<li><p>垂直扩容：购置新的机器，替换已有的机器</p>
</li>
<li><p>水平扩容：直接增加机器</p>
</li>
</ul>
<h4 id="3-1-3-rebalance-自动均衡"><a href="#3-1-3-rebalance-自动均衡" class="headerlink" title="3.1.3 rebalance(自动均衡)"></a>3.1.3 rebalance(自动均衡)</h4><p>增加或减少节点时会自动均衡</p>
<h4 id="3-1-4-master节点"><a href="#3-1-4-master节点" class="headerlink" title="3.1.4 master节点"></a>3.1.4 master节点</h4><p>主节点的主要职责是和集群操作相关的内容，如创建或删除索引，跟踪哪些节点是群集的一部分，并决定哪些分片分配给相关的节点。稳定的主节点对集群的健康是非常重要的。</p>
<h4 id="3-1-5-节点对等"><a href="#3-1-5-节点对等" class="headerlink" title="3.1.5 节点对等"></a>3.1.5 节点对等</h4><p>每个节点都能接收请求<br>每个节点接收到请求后都能把该请求路由到有相关数据的其它节点上<br>接收原始请求的节点负责采集数据并返回给客户端</p>
<h3 id="3-2-分片和副本机制"><a href="#3-2-分片和副本机制" class="headerlink" title="3.2 分片和副本机制"></a>3.2 分片和副本机制</h3><ol>
<li><p>index包含多个shard</p>
</li>
<li><p>每个shard都是一个最小工作单元，承载部分数据；每个shard都是一个lucene实例，有完整的建立索引和处理请求的能力</p>
</li>
<li><p>增减节点时，shard会自动在nodes中负载均衡</p>
</li>
<li><p>primary shard和replica shard，每个document肯定只存在于某一个primary shard以及其对应的replica shard中，不可能存在于多个primary shard</p>
</li>
<li><p>replica shard是primary shard的副本，负责容错，以及承担读请求负载</p>
</li>
<li><p>primary shard的数量在创建索引的时候就固定了，replica shard的数量可以随时修改</p>
</li>
<li><p>primary shard的默认数量是5，replica默认是1，默认有10个shard，5个primary shard，5个replica shard</p>
</li>
<li><p>primary shard不能和自己的replica shard放在同一个节点上（否则节点宕机，primary shard和副本都丢失，起不到容错的作用），但是可以和其他primary shard的replica shard放在同一个节点上</p>
</li>
</ol>
<h3 id="3-3-单节点环境下创建索引分析"><a href="#3-3-单节点环境下创建索引分析" class="headerlink" title="3.3 单节点环境下创建索引分析"></a>3.3 单节点环境下创建索引分析</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">PUT /myindex</span><br><span class="line">&#123;</span><br><span class="line">   <span class="string">&quot;settings&quot;</span> : &#123;</span><br><span class="line">      <span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line">      <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">1</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>这个时候，只会将3个primary shard分配到仅有的一个node上去，另外3个replica shard是无法分配的（一个shard的副本replica，他们两个是不能在同一个节点的）。集群可以正常工作，但是一旦出现节点宕机，数据全部丢失，而且集群不可用，无法接收任何请求。</p>
<h3 id="3-4-两个节点环境下创建索引分析"><a href="#3-4-两个节点环境下创建索引分析" class="headerlink" title="3.4 两个节点环境下创建索引分析"></a>3.4 两个节点环境下创建索引分析</h3><p>将3个primary shard分配到一个node上去，另外3个replica shard分配到另一个节点上</p>
<p>primary shard 和replica shard 保持同步</p>
<p>primary shard 和replica shard 都可以处理客户端的读请求</p>
<h3 id="3-5-水平扩容的过程"><a href="#3-5-水平扩容的过程" class="headerlink" title="3.5 水平扩容的过程"></a>3.5 水平扩容的过程</h3><ol>
<li><p>扩容后primary shard和replica shard会自动的负载均衡</p>
</li>
<li><p>扩容后每个节点上的shard会减少，那么分配给每个shard的CPU，内存，IO资源会更多，性能提高</p>
</li>
<li><p>扩容的极限，如果有6个shard，扩容的极限就是6个节点，每个节点上一个shard，如果想超出扩容的极限，比如说扩容到9个节点，那么可以增加replica shard的个数</p>
</li>
<li><p>6个shard，3个节点，最多能承受几个节点所在的服务器宕机？(容错性)<br>任何一台服务器宕机都会丢失部分数据</p>
</li>
</ol>
<p>为了提高容错性，增加shard的个数：<br>9个shard，(3个primary shard，6个replicashard)，这样就能容忍最多两台服务器宕机了</p>
<p><strong>总结：</strong>扩容是为了提高系统的吞吐量，同时也要考虑容错性，也就是让尽可能多的服务器宕机还能保证数据不丢失</p>
<h3 id="3-6ElasticSearch的容错机制"><a href="#3-6ElasticSearch的容错机制" class="headerlink" title="3.6ElasticSearch的容错机制"></a>3.6ElasticSearch的容错机制</h3><p>以9个shard，3个节点为例：</p>
<p>1.如果master node 宕机，此时不是所有的primary shard都是Active status，所以此时的集群状态是red。</p>
<p>容错处理的第一步:是选举一台服务器作为master<br>容错处理的第二步:新选举出的master会把挂掉的primary shard的某个replica shard 提升为primary shard,此时集群的状态为yellow，因为少了一个replica shard，并不是所有的replica shard都是active status</p>
<p>容错处理的第三步：重启故障机，新master会把所有的副本都复制一份到该节点上，（同步一下宕机后发生的修改），此时集群的状态为green，因为所有的primary shard和replica shard都是Active status</p>
<h3 id="3-7文档的核心元数据"><a href="#3-7文档的核心元数据" class="headerlink" title="3.7文档的核心元数据"></a>3.7文档的核心元数据</h3><p>1._index:</p>
<p>说明了一个文档存储在哪个索引中</p>
<p>同一个索引下存放的是相似的文档(文档的field多数是相同的)</p>
<p>索引名必须是小写的，不能以下划线开头，不能包括逗号</p>
<p>2._type:</p>
<p>表示文档属于索引中的哪个类型</p>
<p>一个索引下只能有一个type</p>
<p>类型名可以是大写也可以是小写的，不能以下划线开头，不能包括逗号</p>
<p>3._id:</p>
<p>文档的唯一标识，和索引，类型组合在一起唯一标识了一个文档</p>
<p>可以手动指定值，也可以由es来生成这个值</p>
<h3 id="3-8-文档id生成方式"><a href="#3-8-文档id生成方式" class="headerlink" title="3.8 文档id生成方式"></a>3.8 文档id生成方式</h3><p>1.手动指定</p>
<p>  put /index/type/66</p>
<p>  通常是把其它系统的已有数据导入到es时</p>
<p>2.由es生成id值</p>
<p>  post /index/type</p>
<p> es生成的id长度为20个字符，使用的是base64编码，URL安全，使用的是GUID算法，分布式下并发生成id值时不会冲突</p>
<h3 id="3-9-source元数据分析"><a href="#3-9-source元数据分析" class="headerlink" title="3.9 _source元数据分析"></a>3.9 _source元数据分析</h3><p>其实就是我们在添加文档时request body中的内容</p>
<p>指定返回的结果中含有哪些字段：</p>
<p>get /index/type/1?_source=name</p>
<h3 id="3-10-改变文档内容原理解析"><a href="#3-10-改变文档内容原理解析" class="headerlink" title="3.10 改变文档内容原理解析"></a>3.10 改变文档内容原理解析</h3><p>替换方式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib/user/<span class="number">4</span></span><br><span class="line">&#123; <span class="string">&quot;first_name&quot;</span> : <span class="string">&quot;Jane&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;last_name&quot;</span> :   <span class="string">&quot;Lucy&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;age&quot;</span> :         <span class="number">24</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;about&quot;</span> :       <span class="string">&quot;I like to collect rock albums&quot;</span>,</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;interests&quot;</span>:  [ <span class="string">&quot;music&quot;</span> ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>修改方式(partial update)：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">2</span>/_update</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;doc&quot;</span>:&#123;</span><br><span class="line">       <span class="string">&quot;age&quot;</span>:<span class="number">26</span></span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>删除文档：标记为deleted，随着数据量的增加，es会选择合适的时间删除掉</p>
<h3 id="3-11-基于groovy脚本执行partial-update"><a href="#3-11-基于groovy脚本执行partial-update" class="headerlink" title="3.11 基于groovy脚本执行partial update"></a>3.11 基于groovy脚本执行partial update</h3><p>es有内置的脚本支持，可以基于groovy脚本实现复杂的操作</p>
<p>1.修改年龄</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">4</span>/_update</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;script&quot;</span>: <span class="string">&quot;ctx._source.age+=1&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>2.修改名字</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">4</span>/_update</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;script&quot;</span>: <span class="string">&quot;ctx._source.last_name+=&#x27;hehe&#x27;&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>3.添加爱好</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">4</span>/_update</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;script&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;source&quot;</span>: <span class="string">&quot;ctx._source.interests.add(params.tag)&quot;</span>,</span><br><span class="line">    <span class="string">&quot;params&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;tag&quot;</span>:<span class="string">&quot;picture&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>4.删除爱好</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">4</span>/_update</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;script&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;source&quot;</span>: <span class="string">&quot;ctx._source.interests.remove(ctx._source.interests.indexOf(params.tag))&quot;</span>,</span><br><span class="line">    <span class="string">&quot;params&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;tag&quot;</span>:<span class="string">&quot;picture&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>5.删除文档</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">4</span>/_update</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;script&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;source&quot;</span>: <span class="string">&quot;ctx.op=ctx._source.age==params.count?&#x27;delete&#x27;:&#x27;none&#x27;&quot;</span>,</span><br><span class="line">    <span class="string">&quot;params&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;count&quot;</span>:<span class="number">29</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>6.upsert</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">4</span>/_update</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;script&quot;</span>: <span class="string">&quot;ctx._source.age += 1&quot;</span>,</span><br><span class="line"></span><br><span class="line">  <span class="string">&quot;upsert&quot;</span>: &#123;</span><br><span class="line">     <span class="string">&quot;first_name&quot;</span> : <span class="string">&quot;Jane&quot;</span>,</span><br><span class="line">     <span class="string">&quot;last_name&quot;</span> :   <span class="string">&quot;Lucy&quot;</span>,</span><br><span class="line">     <span class="string">&quot;age&quot;</span> :  <span class="number">20</span>,</span><br><span class="line">     <span class="string">&quot;about&quot;</span> :       <span class="string">&quot;I like to collect rock albums&quot;</span>,</span><br><span class="line">     <span class="string">&quot;interests&quot;</span>:  [ <span class="string">&quot;music&quot;</span> ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="3-12-partial-update-处理并发冲突"><a href="#3-12-partial-update-处理并发冲突" class="headerlink" title="3.12 partial update 处理并发冲突"></a>3.12 partial update 处理并发冲突</h3><p>使用的是乐观锁:_version</p>
<p>retry_on_conflict:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">POST /lib/user/<span class="number">4</span>/_update?retry_on_conflict=<span class="number">3</span></span><br></pre></td></tr></table></figure>


<p>重新获取文档数据和版本信息进行更新，不断的操作，最多操作的次数就是retry_on_conflict的值</p>
<h3 id="3-13-文档数据路由原理解析"><a href="#3-13-文档数据路由原理解析" class="headerlink" title="3.13 文档数据路由原理解析"></a>3.13 文档数据路由原理解析</h3><p>1.文档路由到分片上：</p>
<p> 一个索引由多个分片构成，当添加(删除，修改)一个文档时，es就需要决定这个文档存储在哪个分片上，这个过程就称为数据路由(routing)</p>
<p>2.路由算法：</p>
<pre><code> shard=hash(routing) % number_of_pirmary_shards
</code></pre>
<p>示例：一个索引，3个primary shard</p>
<p>(1)每次增删改查时，都有一个routing值，默认是文档的_id的值</p>
<p>(2)对这个routing值使用哈希函数进行计算</p>
<p>(3)计算出的值再和主分片个数取余数</p>
<p>余数肯定在0—（number_of_pirmary_shards-1）之间，文档就在对应的shard上</p>
<p>routing值默认是文档的_id的值，也可以手动指定一个值，手动指定对于负载均衡以及提高批量读取的性能都有帮助</p>
<p>3.primary shard个数一旦确定就不能修改了</p>
<h3 id="3-14-文档增删改内部原理"><a href="#3-14-文档增删改内部原理" class="headerlink" title="3.14 文档增删改内部原理"></a>3.14 文档增删改内部原理</h3><p>1:发送增删改请求时，可以选择任意一个节点，该节点就成了协调节点(coordinating node)</p>
<p>2.协调节点使用路由算法进行路由，然后将请求转到primary shard所在节点，该节点处理请求，并把数据同步到它的replica shard</p>
<p>3.协调节点对客户端做出响应</p>
<h3 id="3-15-写一致性原理和quorum机制"><a href="#3-15-写一致性原理和quorum机制" class="headerlink" title="3.15 写一致性原理和quorum机制"></a>3.15 写一致性原理和quorum机制</h3><p>1.任何一个增删改操作都可以跟上一个参数<br>consistency</p>
<p>可以给该参数指定的值：</p>
<p>one: (primary shard)只要有一个primary shard是活跃的就可以执行</p>
<p>all: (all shard)所有的primary shard和replica shard都是活跃的才能执行</p>
<p>quorum: (default) 默认值，大部分shard是活跃的才能执行 （例如共有6个shard，至少有3个shard是活跃的才能执行写操作）</p>
<p>2.quorum机制：多数shard都是可用的，</p>
<p>int((primary+number_of_replica)/2)+1</p>
<p>例如：3个primary shard，1个replica</p>
<p>int((3+1)/2)+1=3</p>
<p>至少3个shard是活跃的</p>
<p>注意：可能出现shard不能分配齐全的情况</p>
<p>比如：1个primary shard,1个replica<br>int((1+1)/2)+1=2<br>但是如果只有一个节点，因为primary shard和replica shard不能在同一个节点上，所以仍然不能执行写操作</p>
<p>再举例：1个primary shard,3个replica,2个节点</p>
<p>int((1+3)/2)+1=3</p>
<p>最后:当活跃的shard的个数没有达到要求时，<br>es默认会等待一分钟，如果在等待的期间活跃的shard的个数没有增加，则显示timeout</p>
<p>put /index/type/id?timeout=60s</p>
<h3 id="3-16-文档查询内部原理"><a href="#3-16-文档查询内部原理" class="headerlink" title="3.16 文档查询内部原理"></a>3.16 文档查询内部原理</h3><p>第一步：查询请求发给任意一个节点，该节点就成了coordinating node，该节点使用路由算法算出文档所在的primary shard</p>
<p>第二步：协调节点把请求转发给primary shard也可以转发给replica shard(使用轮询调度算法(Round-Robin Scheduling，把请求平均分配至primary shard 和replica shard)</p>
<p>第三步：处理请求的节点把结果返回给协调节点，协调节点再返回给应用程序</p>
<p>特殊情况：请求的文档还在建立索引的过程中，primary shard上存在，但replica shar上不存在，但是请求被转发到了replica shard上，这时就会提示找不到文档</p>
<h3 id="3-17-bulk批量操作的json格式解析"><a href="#3-17-bulk批量操作的json格式解析" class="headerlink" title="3.17 bulk批量操作的json格式解析"></a>3.17 bulk批量操作的json格式解析</h3><p>bulk的格式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;action:&#123;metadata&#125;&#125;\n</span><br><span class="line"></span><br><span class="line">&#123;requstbody&#125;\n</span><br></pre></td></tr></table></figure>


<p>为什么不使用如下格式：</p>
<p>[{</p>
<p>“action”: {</p>
<p>},</p>
<p>“data”: {</p>
<p>}</p>
<p>}]</p>
<p>这种方式可读性好，但是内部处理就麻烦了：</p>
<p>1.将json数组解析为JSONArray对象，在内存中就需要有一份json文本的拷贝，另外还有一个JSONArray对象。</p>
<p>2.解析json数组里的每个json，对每个请求中的document进行路由</p>
<p>3.为路由到同一个shard上的多个请求，创建一个请求数组</p>
<p>4.将这个请求数组序列化</p>
<p>5.将序列化后的请求数组发送到对应的节点上去</p>
<p>耗费更多内存，增加java虚拟机开销</p>
<p>1.不用将其转换为json对象，直接按照换行符切割json，内存中不需要json文本的拷贝</p>
<p>2.对每两个一组的json，读取meta，进行document路由</p>
<p>3.直接将对应的json发送到node上去</p>
<h3 id="3-18-查询结果分析"><a href="#3-18-查询结果分析" class="headerlink" title="3.18 查询结果分析"></a>3.18 查询结果分析</h3> <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;took&quot;</span>: <span class="number">419</span>,</span><br><span class="line">  <span class="string">&quot;timed_out&quot;</span>: <span class="keyword">false</span>,</span><br><span class="line">  <span class="string">&quot;_shards&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;total&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;successful&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;skipped&quot;</span>: <span class="number">0</span>,</span><br><span class="line">    <span class="string">&quot;failed&quot;</span>: <span class="number">0</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;hits&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;total&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;max_score&quot;</span>: <span class="number">0.6931472</span>,</span><br><span class="line">    <span class="string">&quot;hits&quot;</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="string">&quot;_index&quot;</span>: <span class="string">&quot;lib3&quot;</span>,</span><br><span class="line">        <span class="string">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">        <span class="string">&quot;_id&quot;</span>: <span class="string">&quot;3&quot;</span>,</span><br><span class="line">        <span class="string">&quot;_score&quot;</span>: <span class="number">0.6931472</span>,</span><br><span class="line">        <span class="string">&quot;_source&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;address&quot;</span>: <span class="string">&quot;bei jing hai dian qu qing he zhen&quot;</span>,</span><br><span class="line">          <span class="string">&quot;name&quot;</span>: <span class="string">&quot;lisi&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="string">&quot;_index&quot;</span>: <span class="string">&quot;lib3&quot;</span>,</span><br><span class="line">        <span class="string">&quot;_type&quot;</span>: <span class="string">&quot;user&quot;</span>,</span><br><span class="line">        <span class="string">&quot;_id&quot;</span>: <span class="string">&quot;2&quot;</span>,</span><br><span class="line">        <span class="string">&quot;_score&quot;</span>: <span class="number">0.47000363</span>,</span><br><span class="line">        <span class="string">&quot;_source&quot;</span>: &#123;</span><br><span class="line">          <span class="string">&quot;address&quot;</span>: <span class="string">&quot;bei jing hai dian qu qing he zhen&quot;</span>,</span><br><span class="line">          <span class="string">&quot;name&quot;</span>: <span class="string">&quot;zhaoming&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>


<p>took：查询耗费的时间，单位是毫秒</p>
<p>_shards：共请求了多少个shard</p>
<p>total：查询出的文档总个数</p>
<p>max_score： 本次查询中，相关度分数的最大值，文档和此次查询的匹配度越高，_score的值越大，排位越靠前</p>
<p>hits：默认查询前10个文档</p>
<p>timed_out：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search?timeout=10ms</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;_source&quot;</span>: [<span class="string">&quot;address&quot;</span>,<span class="string">&quot;name&quot;</span>],</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;changge&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="3-19-多index，多type查询模式"><a href="#3-19-多index，多type查询模式" class="headerlink" title="3.19 多index，多type查询模式"></a>3.19 多index，多type查询模式</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">GET _search</span><br><span class="line"></span><br><span class="line">GET /lib/_search</span><br><span class="line"></span><br><span class="line">GET /lib,lib3/_search</span><br><span class="line"></span><br><span class="line">GET <span class="comment">/*3,*4/_search</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">GET /lib/user/_search</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">GET /lib,lib4/user,items/_search</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">GET /_all/_search</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">GET /_all/user,items/_search</span></span><br></pre></td></tr></table></figure>


<h3 id="3-20-分页查询中的deep-paging问题"><a href="#3-20-分页查询中的deep-paging问题" class="headerlink" title="3.20 分页查询中的deep paging问题"></a>3.20 分页查询中的deep paging问题</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;from&quot;</span>:<span class="number">0</span>,</span><br><span class="line">    <span class="string">&quot;size&quot;</span>:<span class="number">2</span>,</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;terms&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: [<span class="string">&quot;hejiu&quot;</span>,<span class="string">&quot;changge&quot;</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>GET /_search?from=0&amp;size=3</p>
<p>deep paging:查询的很深，比如一个索引有三个primary shard，分别存储了6000条数据，我们要得到第100页的数据(每页10条)，类似这种情况就叫deep paging</p>
<p>如何得到第100页的10条数据？</p>
<p>在每个shard中搜索990到999这10条数据，然后用这30条数据排序，排序之后取10条数据就是要搜索的数据，这种做法是错的，因为3个shard中的数据的_score分数不一样，可能这某一个shard中第一条数据的_score分数比另一个shard中第1000条都要高，所以在每个shard中搜索990到999这10条数据然后排序的做法是不正确的。</p>
<p>正确的做法是每个shard把0到999条数据全部搜索出来（按排序顺序），然后全部返回给coordinate node，由coordinate node按_score分数排序后，取出第100页的10条数据，然后返回给客户端。</p>
<p>deep paging性能问题</p>
<p>1.耗费网络带宽，因为搜索过深的话，各shard要把数据传送给coordinate node，这个过程是有大量数据传递的，消耗网络，</p>
<p>2.消耗内存，各shard要把数据传送给coordinate node，这个传递回来的数据，是被coordinate node保存在内存中的，这样会大量消耗内存。</p>
<p>3.消耗cpu coordinate node要把传回来的数据进行排序，这个排序过程很消耗cpu.</p>
<p>鉴于deep paging的性能问题，所以应尽量减少使用。</p>
<h3 id="3-21-query-string查询及copy-to解析"><a href="#3-21-query-string查询及copy-to解析" class="headerlink" title="3.21 query string查询及copy_to解析"></a>3.21 query string查询及copy_to解析</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search?q=interests:changge</span><br><span class="line"></span><br><span class="line">GET /lib3/user/_search?q=+interests:changge</span><br><span class="line"></span><br><span class="line">GET /lib3/user/_search?q=-interests:changge</span><br></pre></td></tr></table></figure>


<p>copy_to字段是把其它字段中的值，以空格为分隔符组成一个大字符串，然后被分析和索引，但是不存储，也就是说它能被查询，但不能被取回显示。</p>
<p>注意:copy_to指向的字段字段类型要为：text</p>
<p>当没有指定field时，就会从copy_to字段中查询<br>GET /lib3/user/_search?q=changge</p>
<h3 id="3-22字符串排序问题"><a href="#3-22字符串排序问题" class="headerlink" title="3.22字符串排序问题"></a>3.22字符串排序问题</h3><p>对一个字符串类型的字段进行排序通常不准确，因为已经被分词成多个词条了</p>
<p>解决方式：对字段索引两次，一次索引分词（用于搜索），一次索引不分词(用于排序)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/_search</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;sort&quot;</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;interests&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;order&quot;</span>: <span class="string">&quot;desc&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;sort&quot;</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;interests.raw&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;order&quot;</span>: <span class="string">&quot;asc&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DELETE lib3</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib3</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line">        <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span></span><br><span class="line">      &#125;,</span><br><span class="line">     <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;user&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;properties&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;address&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;integer&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;birthday&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;date&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>,</span><br><span class="line">                <span class="string">&quot;fields&quot;</span>: &#123;</span><br><span class="line">                  <span class="string">&quot;raw&quot;</span>:&#123;</span><br><span class="line">                     <span class="string">&quot;type&quot;</span>: <span class="string">&quot;keyword&quot;</span></span><br><span class="line">                   &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                <span class="string">&quot;fielddata&quot;</span>: <span class="keyword">true</span></span><br><span class="line">             &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="3-23-如何计算相关度分数"><a href="#3-23-如何计算相关度分数" class="headerlink" title="3.23 如何计算相关度分数"></a>3.23 如何计算相关度分数</h3><p>使用的是TF/IDF算法(Term Frequency&amp;Inverse Document Frequency)</p>
<p>1.Term Frequency:我们查询的文本中的词条在document本中出现了多少次，出现次数越多，相关度越高</p>
<p>搜索内容： hello world</p>
<p>Hello，I love china.</p>
<p>Hello world,how are you!</p>
<p>2.Inverse Document Frequency：我们查询的文本中的词条在索引的所有文档中出现了多少次，出现的次数越多，相关度越低</p>
<p>搜索内容：hello world</p>
<p>hello，what are you doing?</p>
<p>I like the world.</p>
<p>hello 在索引的所有文档中出现了500次，world出现了100次</p>
<p>3.Field-length(字段长度归约) norm:field越长，相关度越低</p>
<p>搜索内容：hello world</p>
<p>{“title”:”hello,what’s your name?”,”content”:{“owieurowieuolsdjflk”}}</p>
<p>{“title”:”hi,good morning”,”content”:{“lkjkljkj…….world”}}</p>
<p>查看分数是如何计算的：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search?explain=<span class="keyword">true</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;duanlian,changge&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>查看一个文档能否匹配上某个查询：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/<span class="number">2</span>/_explain</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: <span class="string">&quot;duanlian,changge&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="3-24-Doc-Values-解析"><a href="#3-24-Doc-Values-解析" class="headerlink" title="3.24 Doc Values 解析"></a>3.24 Doc Values 解析</h3><p>DocValues其实是Lucene在构建倒排索引时，会额外建立一个有序的正排索引(基于document =&gt; field value的映射列表)</p>
<p>{“birthday”:”1985-11-11”,age:23}</p>
<p>{“birthday”:”1989-11-11”,age:29}</p>
<p>document     age       birthday</p>
<p>doc1         23         1985-11-11</p>
<p>doc2         29         1989-11-11</p>
<p>存储在磁盘上，节省内存 </p>
<p>对排序，分组和一些聚合操作能够大大提升性能 </p>
<p>注意：默认对不分词的字段是开启的，对分词字段无效（需要把fielddata设置为true）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib3</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">     <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;user&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;properties&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;address&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: &#123;</span><br><span class="line">              <span class="string">&quot;type&quot;</span>:<span class="string">&quot;integer&quot;</span>,</span><br><span class="line">              <span class="string">&quot;doc_values&quot;</span>:<span class="keyword">false</span></span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="string">&quot;interests&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;birthday&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;date&quot;</span>&#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="3-25-基于scroll技术滚动搜索大量数据"><a href="#3-25-基于scroll技术滚动搜索大量数据" class="headerlink" title="3.25 基于scroll技术滚动搜索大量数据"></a>3.25 基于scroll技术滚动搜索大量数据</h3><p>如果一次性要查出来比如10万条数据，那么性能会很差，此时一般会采取用scoll滚动查询，一批一批的查，直到所有数据都查询完为止。</p>
<p>1.scoll搜索会在第一次搜索的时候，保存一个当时的视图快照，之后只会基于该旧的视图快照提供数据搜索，如果这个期间数据变更，是不会让用户看到的</p>
<p>2.采用基于_doc(不使用_score)进行排序的方式，性能较高</p>
<p>3.每次发送scroll请求，我们还需要指定一个scoll参数，指定一个时间窗口，每次搜索请求只要在这个时间窗口内能完成就可以了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /lib3/user/_search?scroll=1m</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;sort&quot;</span>:[<span class="string">&quot;_doc&quot;</span>],</span><br><span class="line">  <span class="string">&quot;size&quot;</span>:<span class="number">3</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">GET /_search/scroll</span><br><span class="line">&#123;</span><br><span class="line">   <span class="string">&quot;scroll&quot;</span>: <span class="string">&quot;1m&quot;</span>,</span><br><span class="line">   <span class="string">&quot;scroll_id&quot;</span>: <span class="string">&quot;DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAAAdFkEwRENOVTdnUUJPWVZUd1p2WE5hV2cAAAAAAAAAHhZBMERDTlU3Z1FCT1lWVHdadlhOYVdnAAAAAAAAAB8WQTBEQ05VN2dRQk9ZVlR3WnZYTmFXZw==&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="3-26-dynamic-mapping策略"><a href="#3-26-dynamic-mapping策略" class="headerlink" title="3.26 dynamic mapping策略"></a>3.26 dynamic mapping策略</h3><p><strong>dynamic</strong>:</p>
<p>1.true:遇到陌生字段就 dynamic mapping</p>
<p>2.false:遇到陌生字段就忽略</p>
<p>3.strict:约到陌生字段就报错</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib8</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">     <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;user&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;dynamic&quot;</span>:strict,</span><br><span class="line">        <span class="string">&quot;properties&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;name&quot;</span>: &#123;<span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>&#125;,</span><br><span class="line">            <span class="string">&quot;address&quot;</span>:&#123;</span><br><span class="line">                <span class="string">&quot;type&quot;</span>:<span class="string">&quot;object&quot;</span>,</span><br><span class="line">                <span class="string">&quot;dynamic&quot;</span>:<span class="keyword">true</span></span><br><span class="line">            &#125;,</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>#会报错</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">PUT  /lib8/user/<span class="number">1</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;name&quot;</span>:<span class="string">&quot;lisi&quot;</span>,</span><br><span class="line">  <span class="string">&quot;age&quot;</span>:<span class="number">20</span>,</span><br><span class="line">  <span class="string">&quot;address&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;province&quot;</span>:<span class="string">&quot;beijing&quot;</span>,</span><br><span class="line">    <span class="string">&quot;city&quot;</span>:<span class="string">&quot;beijing&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>date_detection</strong>:默认会按照一定格式识别date，比如yyyy-MM-dd</p>
<p>可以手动关闭某个type的date_detection</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">PUT /lib8</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;number_of_shards&quot;</span> : <span class="number">3</span>,</span><br><span class="line">    <span class="string">&quot;number_of_replicas&quot;</span> : <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">     <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;user&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;date_detection&quot;</span>: <span class="keyword">false</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>定制 dynamic mapping template(type)</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">PUT /my_index</span><br><span class="line">&#123; </span><br><span class="line">  <span class="string">&quot;mappings&quot;</span>: &#123; </span><br><span class="line">    <span class="string">&quot;my_type&quot;</span>: &#123; </span><br><span class="line">      <span class="string">&quot;dynamic_templates&quot;</span>: [ </span><br><span class="line">        &#123; </span><br><span class="line">          <span class="string">&quot;en&quot;</span>: &#123; </span><br><span class="line">            <span class="string">&quot;match&quot;</span>: <span class="string">&quot;*_en&quot;</span>, </span><br><span class="line">            <span class="string">&quot;match_mapping_type&quot;</span>: <span class="string">&quot;string&quot;</span>, </span><br><span class="line">            <span class="string">&quot;mapping&quot;</span>: &#123; </span><br><span class="line">              <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>, </span><br><span class="line">              <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;english&quot;</span> </span><br><span class="line">            &#125; </span><br><span class="line">          &#125; </span><br><span class="line">        &#125; </span><br><span class="line">      ] </span><br><span class="line">     &#125; </span><br><span class="line">  &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>#使用了模板</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">PUT /my_index/my_type/<span class="number">3</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;title_en&quot;</span>: <span class="string">&quot;this is my dog&quot;</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>#没有使用模板</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">PUT /my_index/my_type/<span class="number">5</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;title&quot;</span>: <span class="string">&quot;this is my cat&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET my_index/my_type/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;title&quot;</span>: <span class="string">&quot;is&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="3-27重建索引"><a href="#3-27重建索引" class="headerlink" title="3.27重建索引"></a>3.27重建索引</h3><p>一个field的设置是不能修改的，如果要修改一个field，那么应该重新按照新的mapping，建立一个index，然后将数据批量查询出来，重新用bulk api写入到index中。</p>
<p>批量查询的时候，建议采用scroll api，并且采用多线程并发的方式来reindex数据，每次scroll就查询指定日期的一段数据，交给一个线程即可。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">PUT /index1/type1/<span class="number">4</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="string">&quot;content&quot;</span>:<span class="string">&quot;1990-12-12&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">GET /index1/type1/_search</span><br><span class="line"></span><br><span class="line">GET /index1/type1/_mapping</span><br></pre></td></tr></table></figure>




<p>#报错</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">PUT /index1/type1/<span class="number">4</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="string">&quot;content&quot;</span>:<span class="string">&quot;I am very happy.&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>#修改content的类型为string类型,报错，不允许修改</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">PUT /index1/_mapping/type1</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;properties&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;content&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>#创建一个新的索引，把index1索引中的数据查询出来导入到新的索引中<br>#但是应用程序使用的是之前的索引，为了不用重启应用程序，给index1这个索引起个#别名</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">PUT /index1/_alias/index2</span><br></pre></td></tr></table></figure>


<p>#创建新的索引，把content的类型改为字符串</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">PUT /newindex</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;mappings&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;type1&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;properties&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;content&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>#使用scroll批量查询</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">GET /index1/type1/_search?scroll=1m</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;sort&quot;</span>: [<span class="string">&quot;_doc&quot;</span>],</span><br><span class="line">  <span class="string">&quot;size&quot;</span>: <span class="number">2</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>#使用bulk批量写入新的索引</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">POST /_bulk</span><br><span class="line">&#123;<span class="string">&quot;index&quot;</span>:&#123;<span class="string">&quot;_index&quot;</span>:<span class="string">&quot;newindex&quot;</span>,<span class="string">&quot;_type&quot;</span>:<span class="string">&quot;type1&quot;</span>,<span class="string">&quot;_id&quot;</span>:<span class="number">1</span>&#125;&#125;</span><br><span class="line">&#123;<span class="string">&quot;content&quot;</span>:<span class="string">&quot;1982-12-12&quot;</span>&#125;</span><br></pre></td></tr></table></figure>


<p>#将别名index2和新的索引关联，应用程序不用重启</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">POST /_aliases</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;actions&quot;</span>: [</span><br><span class="line">    &#123;<span class="string">&quot;remove&quot;</span>: &#123;<span class="string">&quot;index&quot;</span>:<span class="string">&quot;index1&quot;</span>,<span class="string">&quot;alias&quot;</span>:<span class="string">&quot;index2&quot;</span>&#125;&#125;,</span><br><span class="line">    &#123;<span class="string">&quot;add&quot;</span>: &#123;<span class="string">&quot;index&quot;</span>: <span class="string">&quot;newindex&quot;</span>,<span class="string">&quot;alias&quot;</span>: <span class="string">&quot;index2&quot;</span>&#125;&#125;</span><br><span class="line">]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">GET index2/type1/_search</span><br></pre></td></tr></table></figure>



<h3 id="3-28-索引不可变的原因"><a href="#3-28-索引不可变的原因" class="headerlink" title="3.28 索引不可变的原因"></a>3.28 索引不可变的原因</h3><p>倒排索引包括：</p>
<p>   文档的列表，文档的数量，词条在每个文档中出现的次数，出现的位置，每个文档的长度，所有文档的平均长度</p>
<p>索引不变的原因：</p>
<p>不需要锁，提升了并发性能</p>
<p>可以一直保存在缓存中（filter）</p>
<p>节省cpu和io开销</p>
<h2 id="第四节-在Java应用中访问ElasticSearch"><a href="#第四节-在Java应用中访问ElasticSearch" class="headerlink" title="第四节 在Java应用中访问ElasticSearch"></a>第四节 在Java应用中访问ElasticSearch</h2><h3 id="4-1在Java应用中实现查询文档"><a href="#4-1在Java应用中实现查询文档" class="headerlink" title="4.1在Java应用中实现查询文档"></a>4.1在Java应用中实现查询文档</h3><p>pom中加入ElasticSearch6.2.4的依赖：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependencies&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">    	&lt;groupId&gt;org.elasticsearch.client&lt;&#x2F;groupId&gt;</span><br><span class="line">    	&lt;artifactId&gt;transport&lt;&#x2F;artifactId&gt;</span><br><span class="line">     	&lt;version&gt;6.2.4&lt;&#x2F;version&gt;</span><br><span class="line">    &lt;&#x2F;dependency&gt;    </span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">  		&lt;groupId&gt;junit&lt;&#x2F;groupId&gt;</span><br><span class="line">  		&lt;artifactId&gt;junit&lt;&#x2F;artifactId&gt;</span><br><span class="line">  		&lt;version&gt;4.12&lt;&#x2F;version&gt;</span><br><span class="line">  		&lt;scope&gt;test&lt;&#x2F;scope&gt;</span><br><span class="line">	&lt;&#x2F;dependency&gt;</span><br><span class="line">&lt;&#x2F;dependencies&gt; </span><br><span class="line">&lt;build&gt;</span><br><span class="line">      &lt;plugins&gt;</span><br><span class="line">			&lt;!-- java编译插件 --&gt;</span><br><span class="line">			&lt;plugin&gt;</span><br><span class="line">				&lt;groupId&gt;org.apache.maven.plugins&lt;&#x2F;groupId&gt;</span><br><span class="line">				&lt;artifactId&gt;maven-compiler-plugin&lt;&#x2F;artifactId&gt;</span><br><span class="line">				&lt;version&gt;3.2&lt;&#x2F;version&gt;</span><br><span class="line">				&lt;configuration&gt;</span><br><span class="line">					&lt;source&gt;1.8&lt;&#x2F;source&gt;</span><br><span class="line">					&lt;target&gt;1.8&lt;&#x2F;target&gt;</span><br><span class="line">					&lt;encoding&gt;UTF-8&lt;&#x2F;encoding&gt;</span><br><span class="line">				&lt;&#x2F;configuration&gt;</span><br><span class="line">			&lt;&#x2F;plugin&gt;</span><br><span class="line">		&lt;&#x2F;plugins&gt;</span><br><span class="line">  &lt;&#x2F;build&gt;  </span><br></pre></td></tr></table></figure>
<p>创建ESClient工具类，生产Client</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EsClient</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span>   TransportClient <span class="title">getClient</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//指定ES集群</span></span><br><span class="line">        Settings settings = Settings.builder()</span><br><span class="line">                .put(<span class="string">&quot;cluster.name&quot;</span>,<span class="string">&quot;my-application&quot;</span>)</span><br><span class="line">                .put(<span class="string">&quot;client.transport.sniff&quot;</span>, <span class="keyword">true</span>)</span><br><span class="line">                .build();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            client = <span class="keyword">new</span> PreBuiltTransportClient(settings)</span><br><span class="line">                    .addTransportAddress(<span class="keyword">new</span> TransportAddress(InetAddress.getByName(<span class="string">&quot;192.168.127.131&quot;</span>), <span class="number">9300</span>));</span><br><span class="line">        &#125; <span class="keyword">catch</span> (UnknownHostException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> client;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>数据查询方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">selectEs</span><span class="params">()</span> <span class="keyword">throws</span> UnknownHostException </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client  = EsClient.getClient();</span><br><span class="line">        <span class="comment">//实现数据查询</span></span><br><span class="line">        GetResponse response = client.prepareGet(<span class="string">&quot;lib&quot;</span>,<span class="string">&quot;user&quot;</span>,<span class="string">&quot;1&quot;</span>).execute().actionGet();</span><br><span class="line">        String sourceAsString = response.getSourceAsString();</span><br><span class="line">        System.out.println(sourceAsString);</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-2-在Java应用中实现添加文档"><a href="#4-2-在Java应用中实现添加文档" class="headerlink" title="4.2 在Java应用中实现添加文档"></a>4.2 在Java应用中实现添加文档</h3><p>在es中添加下面字段</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> <span class="string">&quot;&#123;&quot;</span> +</span><br><span class="line">   <span class="string">&quot;\&quot;id\&quot;:\&quot;1\&quot;,&quot;</span> +</span><br><span class="line">   <span class="string">&quot;\&quot;title\&quot;:\&quot; \&quot;,&quot;</span> +</span><br><span class="line">   <span class="string">&quot;\&quot;content\&quot;:\&quot;在不必改变原类文件和使用继承的情况下，动态地扩展一个对象的功能。\&quot;,&quot;</span> +</span><br><span class="line">   <span class="string">&quot;\&quot;postdate\&quot;:\&quot;2018-05-20 14:38:00\&quot;,&quot;</span> +</span><br><span class="line">   <span class="string">&quot;\&quot;url\&quot;:\&quot;csdn.net/79239072\&quot;&quot;</span> +</span><br><span class="line"><span class="string">&quot;&#125;&quot;</span></span><br></pre></td></tr></table></figure>
<p>经分析，需要创建索引类型属性，首先需要执行添加</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">PUT /index1</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;settings&quot;</span>:&#123;</span><br><span class="line">     <span class="string">&quot;number_of_shards&quot;</span>:<span class="number">3</span>,</span><br><span class="line">     <span class="string">&quot;number_of_replicas&quot;</span>:<span class="number">0</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;mappings&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;blog&quot;</span>:&#123;</span><br><span class="line">      <span class="string">&quot;properties&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;id&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>:<span class="string">&quot;long&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;title&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>,</span><br><span class="line">          <span class="string">&quot;analyzer&quot;</span>:<span class="string">&quot;ik_max_word&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;content&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>,</span><br><span class="line">          <span class="string">&quot;analyzer&quot;</span>:<span class="string">&quot;ik_max_word&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;postdate&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>: <span class="string">&quot;date&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;url&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">insertEs</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client  = EsClient.getClient();</span><br><span class="line">        XContentBuilder doc = XContentFactory</span><br><span class="line">                .jsonBuilder()</span><br><span class="line">                .startObject()</span><br><span class="line">                .field(<span class="string">&quot;id&quot;</span>,<span class="string">&quot;1&quot;</span>)</span><br><span class="line">                .field(<span class="string">&quot;title&quot;</span>,<span class="string">&quot;Java设计模式之装饰模式&quot;</span>)</span><br><span class="line">                .field(<span class="string">&quot;content&quot;</span>,<span class="string">&quot;在不必改变原类文件和使用继承的情况下，动态地扩展一个对象的功能。&quot;</span>)</span><br><span class="line">                .field(<span class="string">&quot;postdate&quot;</span>,<span class="string">&quot;2018-05-20&quot;</span>)</span><br><span class="line">                .field(<span class="string">&quot;url&quot;</span>,<span class="string">&quot;csdn.net/79239072&quot;</span>)</span><br><span class="line">                .endObject();</span><br><span class="line">        <span class="comment">//添加文档</span></span><br><span class="line">        IndexResponse indexResponse = client.prepareIndex(<span class="string">&quot;index1&quot;</span>, <span class="string">&quot;blog&quot;</span>, <span class="string">&quot;10 &quot;</span>)</span><br><span class="line">                .setSource(doc).get();</span><br><span class="line">        </span><br><span class="line"></span><br><span class="line">		System.out.println(indexResponse.status());</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


<h3 id="4-3在Java应用中实现删除文档"><a href="#4-3在Java应用中实现删除文档" class="headerlink" title="4.3在Java应用中实现删除文档"></a>4.3在Java应用中实现删除文档</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">DeleteResponse response = client.prepareDelete(<span class="string">&quot;index1&quot;</span>,<span class="string">&quot;blog&quot;</span>,<span class="string">&quot;SzYJjWMBjSAutsuLRP_P&quot;</span>).get();</span><br><span class="line"></span><br><span class="line"><span class="comment">//删除成功返回OK，否则返回NOT_FOUND</span></span><br><span class="line">System.out.println(response.status());</span><br></pre></td></tr></table></figure>




<h3 id="4-4在Java应用中实现更新文档"><a href="#4-4在Java应用中实现更新文档" class="headerlink" title="4.4在Java应用中实现更新文档"></a>4.4在Java应用中实现更新文档</h3> <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">UpdateRequest request=<span class="keyword">new</span> UpdateRequest();</span><br><span class="line">        request.index(<span class="string">&quot;index1&quot;</span>)</span><br><span class="line">                .type(<span class="string">&quot;blog&quot;</span>)</span><br><span class="line">                .id(<span class="string">&quot;2&quot;</span>)</span><br><span class="line">                .doc(</span><br><span class="line">                		XContentFactory.jsonBuilder().startObject()</span><br><span class="line">                        .field(<span class="string">&quot;title&quot;</span>,<span class="string">&quot;单例模式解读&quot;</span>)</span><br><span class="line">                        .endObject()</span><br><span class="line">                );</span><br><span class="line">UpdateResponse response=client.update(request).get();</span><br><span class="line"></span><br><span class="line"><span class="comment">//更新成功返回OK，否则返回NOT_FOUND</span></span><br><span class="line"></span><br><span class="line">System.out.println(response.status());</span><br></pre></td></tr></table></figure>
<p>upsert方式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">IndexRequest request1 =<span class="keyword">new</span> IndexRequest(<span class="string">&quot;index1&quot;</span>,<span class="string">&quot;blog&quot;</span>,<span class="string">&quot;3&quot;</span>)</span><br><span class="line">                .source(</span><br><span class="line">                		XContentFactory.jsonBuilder().startObject()</span><br><span class="line">                                .field(<span class="string">&quot;id&quot;</span>,<span class="string">&quot;3&quot;</span>)</span><br><span class="line">                                .field(<span class="string">&quot;title&quot;</span>,<span class="string">&quot;装饰模式&quot;</span>)</span><br><span class="line">                                .field(<span class="string">&quot;content&quot;</span>,<span class="string">&quot;动态地扩展一个对象的功能&quot;</span>)</span><br><span class="line">                                .field(<span class="string">&quot;postdate&quot;</span>,<span class="string">&quot;2018-05-23&quot;</span>)</span><br><span class="line">                                .field(<span class="string">&quot;url&quot;</span>,<span class="string">&quot;csdn.net/79239072&quot;</span>)</span><br><span class="line">                                .endObject()</span><br><span class="line">                );</span><br><span class="line">        UpdateRequest request2=<span class="keyword">new</span> UpdateRequest(<span class="string">&quot;index1&quot;</span>,<span class="string">&quot;blog&quot;</span>,<span class="string">&quot;3&quot;</span>)</span><br><span class="line">                .doc(</span><br><span class="line">                		XContentFactory.jsonBuilder().startObject()</span><br><span class="line">                        .field(<span class="string">&quot;title&quot;</span>,<span class="string">&quot;装饰模式解读&quot;</span>)</span><br><span class="line">                        .endObject()</span><br><span class="line">                ).upsert(request1);</span><br><span class="line">        </span><br><span class="line">UpdateResponse response=client.update(request2).get();</span><br><span class="line">        </span><br><span class="line"><span class="comment">//upsert操作成功返回OK，否则返回NOT_FOUND</span></span><br><span class="line">System.out.println(response.status());</span><br></pre></td></tr></table></figure>



<h3 id="4-5在Java应用中实现批量操作"><a href="#4-5在Java应用中实现批量操作" class="headerlink" title="4.5在Java应用中实现批量操作"></a>4.5在Java应用中实现批量操作</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">MultiGetResponse mgResponse = client.prepareMultiGet()</span><br><span class="line">	                .add(<span class="string">&quot;index1&quot;</span>,<span class="string">&quot;blog&quot;</span>,<span class="string">&quot;3&quot;</span>,<span class="string">&quot;2&quot;</span>)</span><br><span class="line">	                .add(<span class="string">&quot;lib3&quot;</span>,<span class="string">&quot;user&quot;</span>,<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;3&quot;</span>)</span><br><span class="line">	                .get();</span><br><span class="line">		    </span><br><span class="line"><span class="keyword">for</span>(MultiGetItemResponse response:mgResponse)&#123;</span><br><span class="line">	            GetResponse rp=response.getResponse();</span><br><span class="line">	            <span class="keyword">if</span>(rp!=<span class="keyword">null</span> &amp;&amp; rp.isExists())&#123;</span><br><span class="line">	                System.out.println(rp.getSourceAsString());</span><br><span class="line">	            &#125;</span><br><span class="line">	        &#125;</span><br></pre></td></tr></table></figure>
<p>bulk：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">BulkRequestBuilder bulkRequest = client.prepareBulk();</span><br><span class="line">bulkRequest.add(client.prepareIndex(<span class="string">&quot;lib2&quot;</span>, <span class="string">&quot;books&quot;</span>, <span class="string">&quot;4&quot;</span>)</span><br><span class="line">                .setSource(XContentFactory.jsonBuilder()</span><br><span class="line">                        .startObject()</span><br><span class="line">                        .field(<span class="string">&quot;title&quot;</span>, <span class="string">&quot;python&quot;</span>)</span><br><span class="line">                        .field(<span class="string">&quot;price&quot;</span>, <span class="number">68</span>)</span><br><span class="line">                        .endObject()</span><br><span class="line">                )</span><br><span class="line">        );</span><br><span class="line">bulkRequest.add(client.prepareIndex(<span class="string">&quot;lib2&quot;</span>, <span class="string">&quot;books&quot;</span>, <span class="string">&quot;5&quot;</span>)</span><br><span class="line">                .setSource(XContentFactory.jsonBuilder()</span><br><span class="line">                        .startObject()</span><br><span class="line">                        .field(<span class="string">&quot;title&quot;</span>, <span class="string">&quot;VR&quot;</span>)</span><br><span class="line">                        .field(<span class="string">&quot;price&quot;</span>, <span class="number">38</span>)</span><br><span class="line">                        .endObject()</span><br><span class="line">                )</span><br><span class="line">        );</span><br><span class="line">        <span class="comment">//批量执行</span></span><br><span class="line">BulkResponse bulkResponse = bulkRequest.get();</span><br><span class="line">        </span><br><span class="line">System.out.println(bulkResponse.status());</span><br><span class="line"><span class="keyword">if</span> (bulkResponse.hasFailures()) &#123;</span><br><span class="line">     System.out.println(<span class="string">&quot;存在失败操作&quot;</span>);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-6-查询删除"><a href="#4-6-查询删除" class="headerlink" title="4.6 查询删除"></a>4.6 查询删除</h2><p>首先进行查询，然后进行删除</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span>  <span class="keyword">static</span>  <span class="keyword">void</span> <span class="title">selectAndeDelete</span><span class="params">()</span></span>&#123;</span><br><span class="line">       <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">       TransportClient client  = EsClient.getClient();</span><br><span class="line"></span><br><span class="line">       BulkByScrollResponse bulkByScrollResponse = DeleteByQueryAction.INSTANCE</span><br><span class="line">               .newRequestBuilder(client)</span><br><span class="line">               .filter(QueryBuilders.matchQuery(<span class="string">&quot;title&quot;</span>, <span class="string">&quot;工厂&quot;</span>))</span><br><span class="line">               .source(<span class="string">&quot;index1&quot;</span>)</span><br><span class="line">               .get();</span><br><span class="line">       <span class="keyword">long</span> deleted = bulkByScrollResponse.getDeleted();</span><br><span class="line">       System.out.println(deleted);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-7-查询所有"><a href="#4-7-查询所有" class="headerlink" title="4.7 查询所有"></a>4.7 查询所有</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">selectAll</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client  = EsClient.getClient();</span><br><span class="line">        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();</span><br><span class="line">        SearchResponse lib1 = client.prepareSearch(<span class="string">&quot;lib1&quot;</span>)</span><br><span class="line">                .setQuery(matchAllQueryBuilder)</span><br><span class="line">                .setSize(<span class="number">3</span>)</span><br><span class="line">                .get();</span><br><span class="line">        SearchHits hits= lib1.getHits();</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit:hits)&#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line"></span><br><span class="line">            Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();</span><br><span class="line">            <span class="keyword">for</span> (String key: sourceAsMap.keySet())&#123;</span><br><span class="line">                System.out.println(key+<span class="string">&quot;=&quot;</span>+sourceAsMap.get(key));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-8-match-query-查询"><a href="#4-8-match-query-查询" class="headerlink" title="4.8 match query 查询"></a>4.8 match query 查询</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">matchQuery</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client  = EsClient.getClient();</span><br><span class="line">        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(<span class="string">&quot;insterests&quot;</span>, <span class="string">&quot;changer&quot;</span>);</span><br><span class="line">        SearchResponse lib1 = client.prepareSearch(<span class="string">&quot;lib12&quot;</span>)</span><br><span class="line">                .setQuery(matchQueryBuilder)</span><br><span class="line">                .setSize(<span class="number">3</span>)</span><br><span class="line">                .get();</span><br><span class="line">        SearchHits hits= lib1.getHits();</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit:hits)&#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line"></span><br><span class="line">            Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();</span><br><span class="line">            <span class="keyword">for</span> (String key: sourceAsMap.keySet())&#123;</span><br><span class="line">                System.out.println(key+<span class="string">&quot;=&quot;</span>+sourceAsMap.get(key));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-9-multiMachQuery查询"><a href="#4-9-multiMachQuery查询" class="headerlink" title="4.9  multiMachQuery查询"></a>4.9  multiMachQuery查询</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">multiMachQuery</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client  = EsClient.getClient();</span><br><span class="line">        </span><br><span class="line">        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(<span class="string">&quot;changge&quot;</span>, <span class="string">&quot;address&quot;</span>, <span class="string">&quot;insterests&quot;</span>);</span><br><span class="line">        </span><br><span class="line">        SearchResponse lib1 = client.prepareSearch(<span class="string">&quot;lib12&quot;</span>)</span><br><span class="line">                .setQuery(multiMatchQueryBuilder)</span><br><span class="line">                .setSize(<span class="number">3</span>)</span><br><span class="line">                .get();</span><br><span class="line">        </span><br><span class="line">        SearchHits hits= lib1.getHits();</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit:hits)&#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line">            Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();</span><br><span class="line">            <span class="keyword">for</span> (String key: sourceAsMap.keySet())&#123;</span><br><span class="line">                System.out.println(key+<span class="string">&quot;=&quot;</span>+sourceAsMap.get(key));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-10-term查询"><a href="#4-10-term查询" class="headerlink" title="4.10 term查询"></a>4.10 term查询</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">termQuery</span><span class="params">()</span> </span>&#123;</span><br><span class="line">     <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">     TransportClient client = EsClient.getClient();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">     TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(<span class="string">&quot;insterests&quot;</span>, <span class="string">&quot;changge&quot;</span>);</span><br><span class="line"></span><br><span class="line">     SearchResponse lib3 = client.prepareSearch(<span class="string">&quot;lib3&quot;</span>)</span><br><span class="line">             .setQuery(termQueryBuilder)</span><br><span class="line">             .setSize(<span class="number">3</span>)</span><br><span class="line">             .get();</span><br><span class="line">     SearchHits hits = lib3.getHits();</span><br><span class="line">     <span class="keyword">for</span> (SearchHit hit : hits) &#123;</span><br><span class="line">         System.out.println(hit.getSourceAsString());</span><br><span class="line">         Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();</span><br><span class="line">         <span class="keyword">for</span> (String key : sourceAsMap.keySet()) &#123;</span><br><span class="line">             System.out.println(key + <span class="string">&quot;=&quot;</span> + sourceAsMap.get(key));</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">     client.close();</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-11-terms查询"><a href="#4-11-terms查询" class="headerlink" title="4.11 terms查询"></a>4.11 terms查询</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">termsQuery</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = EsClient.getClient();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(<span class="string">&quot;insterests&quot;</span>, <span class="string">&quot;changge&quot;</span>,<span class="string">&quot;lvyou&quot;</span>);</span><br><span class="line"></span><br><span class="line">        SearchResponse lib3 = client.prepareSearch(<span class="string">&quot;lib3&quot;</span>)</span><br><span class="line">                .setQuery(termsQueryBuilder)</span><br><span class="line">                .setSize(<span class="number">3</span>)</span><br><span class="line">                .get();</span><br><span class="line">        SearchHits hits = lib3.getHits();</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit : hits) &#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line">            Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();</span><br><span class="line">            <span class="keyword">for</span> (String key : sourceAsMap.keySet()) &#123;</span><br><span class="line">                System.out.println(key + <span class="string">&quot;=&quot;</span> + sourceAsMap.get(key));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-12-其他查询"><a href="#4-12-其他查询" class="headerlink" title="4.12 其他查询"></a>4.12 其他查询</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//限定查询</span></span><br><span class="line">       RangeQueryBuilder birthday = QueryBuilders.rangeQuery(<span class="string">&quot;birthday&quot;</span>).from(<span class="string">&quot;1990-1-1&quot;</span>).to(<span class="string">&quot;2000-12-30&quot;</span>).format(<span class="string">&quot;yyyy-MM-dd&quot;</span>);</span><br><span class="line">       <span class="comment">//前缀查询</span></span><br><span class="line">       PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;zhao&quot;</span>);</span><br><span class="line">       <span class="comment">//模糊查询</span></span><br><span class="line">       WildcardQueryBuilder name = QueryBuilders.wildcardQuery(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;zhao*&quot;</span>);</span><br><span class="line">       <span class="comment">//模糊查询</span></span><br><span class="line">       FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery(<span class="string">&quot;interests&quot;</span>, <span class="string">&quot;changge&quot;</span>);</span><br><span class="line">       <span class="comment">//类型查询</span></span><br><span class="line">       TypeQueryBuilder blog = QueryBuilders.typeQuery(<span class="string">&quot;blog&quot;</span>);</span><br><span class="line">       <span class="comment">//id查询</span></span><br><span class="line">       IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery().addIds(<span class="string">&quot;1&quot;</span>, <span class="string">&quot;3&quot;</span>);</span><br></pre></td></tr></table></figure>


<h2 id="4-13-聚合查询"><a href="#4-13-聚合查询" class="headerlink" title="4.13 聚合查询"></a>4.13 聚合查询</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">public static  void  agg()&#123;</span><br><span class="line">        &#x2F;&#x2F; 创建访问es服务器的客户端</span><br><span class="line">        TransportClient client &#x3D; EsClient.getClient();</span><br><span class="line">        MaxAggregationBuilder max &#x3D; AggregationBuilders.max(&quot;aggMax&quot;).field(&quot;age&quot;);</span><br><span class="line">        MinAggregationBuilder min &#x3D; AggregationBuilders.min(&quot;aggMin&quot;).field(&quot;age&quot;);</span><br><span class="line">        AvgAggregationBuilder avg &#x3D; AggregationBuilders.avg(&quot;aggAvg&quot;).field(&quot;age&quot;);</span><br><span class="line">        SumAggregationBuilder sum &#x3D; AggregationBuilders.sum(&quot;aggSum&quot;).field(&quot;age&quot;);</span><br><span class="line">        CardinalityAggregationBuilder cardinality &#x3D; AggregationBuilders.cardinality(&quot;aggCardinality&quot;).field(&quot;age&quot;);</span><br><span class="line">        SearchResponse lib3 &#x3D; client.prepareSearch(&quot;lib3&quot;).addAggregation(max).get();</span><br><span class="line">        Max aggMax &#x3D; lib3.getAggregations().get(&quot;aggMax&quot;);</span><br><span class="line">        double value &#x3D; aggMax.getValue();</span><br><span class="line">        System.out.println(value);</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-14-QueryString-查询"><a href="#4-14-QueryString-查询" class="headerlink" title="4.14 QueryString 查询"></a>4.14 QueryString 查询</h2> <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">stringQuery</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = EsClient.getClient();</span><br><span class="line">        CommonTermsQueryBuilder commonTermsQueryBuilder = QueryBuilders.commonTermsQuery(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;zhaoliu1&quot;</span>);</span><br><span class="line">        QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(<span class="string">&quot;+changge  -hejiu&quot;</span>);</span><br><span class="line">        SimpleQueryStringBuilder simpleQueryStringBuilder = QueryBuilders.simpleQueryStringQuery(<span class="string">&quot;+changge  -hejiu&quot;</span>);</span><br><span class="line">        SearchResponse lib = client.prepareSearch(<span class="string">&quot;lib&quot;</span>)</span><br><span class="line">                .setQuery(commonTermsQueryBuilder)</span><br><span class="line">                .get();</span><br><span class="line">        SearchHits hits = lib.getHits();</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit : hits) &#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line">            Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();</span><br><span class="line">            <span class="keyword">for</span> (String key : sourceAsMap.keySet()) &#123;</span><br><span class="line">                System.out.println(key + <span class="string">&quot;=&quot;</span> + sourceAsMap.get(key));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>




<h2 id="4-15-组合查询"><a href="#4-15-组合查询" class="headerlink" title="4.15 组合查询"></a>4.15 组合查询</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">boolQuery</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = EsClient.getClient();</span><br><span class="line">        BoolQueryBuilder filter = QueryBuilders.boolQuery()</span><br><span class="line">                .must(QueryBuilders.matchQuery(<span class="string">&quot;instart&quot;</span>, <span class="string">&quot;change&quot;</span>))</span><br><span class="line">                .mustNot(QueryBuilders.matchQuery(<span class="string">&quot;instert&quot;</span>, <span class="string">&quot;lvyou&quot;</span>))</span><br><span class="line">                .should(QueryBuilders.matchQuery(<span class="string">&quot;address&quot;</span>, <span class="string">&quot;beijing&quot;</span>))</span><br><span class="line">                .filter(QueryBuilders.rangeQuery(<span class="string">&quot;birthday&quot;</span>).gte(<span class="string">&quot;1992-1-1&quot;</span>).lte(<span class="string">&quot;1993-12-1&quot;</span>).format(<span class="string">&quot;yyyy-MM-dd&quot;</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        SearchResponse lib = client.prepareSearch(<span class="string">&quot;lib&quot;</span>)</span><br><span class="line">                .setQuery(filter)</span><br><span class="line">                .get();</span><br><span class="line">        SearchHits hits = lib.getHits();</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit : hits) &#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line">            Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();</span><br><span class="line">            <span class="keyword">for</span> (String key : sourceAsMap.keySet()) &#123;</span><br><span class="line">                System.out.println(key + <span class="string">&quot;=&quot;</span> + sourceAsMap.get(key));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>不计算相关度分数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//不计算相关度分数</span></span><br><span class="line">        ConstantScoreQueryBuilder constantScoreQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;zhaoliu&quot;</span>));</span><br></pre></td></tr></table></figure>


<h2 id="4-16-分组聚合（统计）"><a href="#4-16-分组聚合（统计）" class="headerlink" title="4.16 分组聚合（统计）"></a>4.16 分组聚合（统计）</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">te</span><span class="params">()</span></span>&#123;</span><br><span class="line">     <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">     TransportClient client = EsClient.getClient();</span><br><span class="line"></span><br><span class="line">     TermsAggregationBuilder field = AggregationBuilders.terms(<span class="string">&quot;terms&quot;</span>).field(<span class="string">&quot;age&quot;</span>);</span><br><span class="line">     SearchResponse lib = client.prepareSearch(<span class="string">&quot;lib&quot;</span>).addAggregation(field).execute().actionGet();</span><br><span class="line">     Aggregation filter1 = lib.getAggregations().get(<span class="string">&quot;terms&quot;</span>);</span><br><span class="line">     System.out.println(filter1);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>


<h2 id="4-17-过滤聚合"><a href="#4-17-过滤聚合" class="headerlink" title="4.17 过滤聚合"></a>4.17 过滤聚合</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">te</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = EsClient.getClient();</span><br><span class="line">        TermQueryBuilder age = QueryBuilders.termQuery(<span class="string">&quot;age&quot;</span>, <span class="number">20</span>);</span><br><span class="line">        FilterAggregationBuilder filter = AggregationBuilders.filter(<span class="string">&quot;Filter&quot;</span>, age);</span><br><span class="line">        SearchResponse lib = client.prepareSearch(<span class="string">&quot;lib&quot;</span>).addAggregation(filter).execute().actionGet();</span><br><span class="line">        Aggregation filter1 = lib.getAggregations().get(<span class="string">&quot;filter&quot;</span>);</span><br><span class="line">        System.out.println(filter1);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


<h2 id="4-18-range过滤"><a href="#4-18-range过滤" class="headerlink" title="4.18 range过滤"></a>4.18 range过滤</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">te</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = EsClient.getClient();</span><br><span class="line">        RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders</span><br><span class="line">                .range(<span class="string">&quot;range&quot;</span>)</span><br><span class="line">                .field(<span class="string">&quot;age&quot;</span>)</span><br><span class="line">                .addUnboundedTo(<span class="number">50</span>)    <span class="comment">//(  .to)</span></span><br><span class="line">                .addRange(<span class="number">20</span>, <span class="number">50</span>)      <span class="comment">//[from,to)</span></span><br><span class="line">                .addUnboundedFrom(<span class="number">25</span>); <span class="comment">//[from,  ）</span></span><br><span class="line">        SearchResponse lib = client.prepareSearch(<span class="string">&quot;lib&quot;</span>)</span><br><span class="line">                .addAggregation(rangeAggregationBuilder).execute().actionGet();</span><br><span class="line">        Range range = lib.getAggregations().get(<span class="string">&quot;range&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (Range.Bucket entry:range.getBuckets())&#123;</span><br><span class="line">            System.out.println(entry.getKey()+<span class="string">&quot;:&quot;</span>+entry.getDocCount());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>统计为空</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">te</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = EsClient.getClient();</span><br><span class="line">        MissingAggregationBuilder field = AggregationBuilders</span><br><span class="line">                .missing(<span class="string">&quot;missing&quot;</span>).field(<span class="string">&quot;price&quot;</span>);</span><br><span class="line">        SearchResponse lib = client.prepareSearch(<span class="string">&quot;lib&quot;</span>)</span><br><span class="line">                .addAggregation(field).execute().actionGet();</span><br><span class="line">        Aggregation missing = lib.getAggregations().get(<span class="string">&quot;missing&quot;</span>);</span><br><span class="line">        System.out.println(missing.toString());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h2 id="ES管理"><a href="#ES管理" class="headerlink" title="ES管理"></a>ES管理</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">setting</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建访问es服务器的客户端</span></span><br><span class="line">        TransportClient client = EsClient.getClient();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取集群信息</span></span><br><span class="line">        ClusterHealthResponse clusterHealthResponse = client.admin().cluster().prepareHealth().get();</span><br><span class="line">        <span class="comment">//获取集群名称</span></span><br><span class="line">        String clusterName = clusterHealthResponse.getClusterName();</span><br><span class="line">        <span class="comment">//获取集群节点数量</span></span><br><span class="line">        <span class="keyword">int</span> numberOfDataNodes = clusterHealthResponse.getNumberOfDataNodes();</span><br><span class="line">        <span class="comment">//获取索引</span></span><br><span class="line">        Collection&lt;ClusterIndexHealth&gt; values = clusterHealthResponse.getIndices().values();</span><br><span class="line">        <span class="keyword">for</span> (ClusterIndexHealth health :values)&#123;</span><br><span class="line">            <span class="comment">//当前索引的名称</span></span><br><span class="line">            String index = health.getIndex();</span><br><span class="line">            <span class="comment">//此索引有多少个主分片</span></span><br><span class="line">            <span class="keyword">int</span> numberOfShards = health.getNumberOfShards();</span><br><span class="line">            <span class="comment">//此索引有多少个副本</span></span><br><span class="line">            <span class="keyword">int</span> numberOfReplicas = health.getNumberOfReplicas();</span><br><span class="line">            <span class="comment">//索引的健康状况 ，颜色区分</span></span><br><span class="line">            ClusterHealthStatus status = health.getStatus();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/java/" rel="tag"># java</a>
              <a href="/tags/linux/" rel="tag"># linux</a>
              <a href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="tag"># 数据库</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/01/03/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/03_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E5%92%8C%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/" rel="prev" title="03_SHELL编程之条件判断和流程控制">
      <i class="fa fa-chevron-left"></i> 03_SHELL编程之条件判断和流程控制
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/04_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5/" rel="next" title="04_SHELL编程之循环语句">
      04_SHELL编程之循环语句 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AC%AC%E4%B8%80%E7%AB%A0-ElasticSearch%E5%85%A5%E9%97%A8%E7%AF%87"><span class="nav-number">1.</span> <span class="nav-text">第一章 ElasticSearch入门篇</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AC%AC%E4%B8%80%E8%8A%82-ElasticSearch%E6%A6%82%E8%BF%B0"><span class="nav-number">1.1.</span> <span class="nav-text">第一节 ElasticSearch概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-ElasticSearch-%E7%AE%80%E4%BB%8B"><span class="nav-number">1.1.1.</span> <span class="nav-text">1.1 ElasticSearch 简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2ElasticSearch%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">1.1.2.</span> <span class="nav-text">1.2ElasticSearch的基本概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3Elasticsearch%E7%9A%84%E6%9E%B6%E6%9E%84"><span class="nav-number">1.1.3.</span> <span class="nav-text">1.3Elasticsearch的架构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4RESTfull-API"><span class="nav-number">1.1.4.</span> <span class="nav-text">1.4RESTfull API</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5CRUL%E5%91%BD%E4%BB%A4"><span class="nav-number">1.1.5.</span> <span class="nav-text">1.5CRUL命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6CentOS7%E4%B8%8B%E5%AE%89%E8%A3%85ElasticSearch6-2-4"><span class="nav-number">1.1.6.</span> <span class="nav-text">1.6CentOS7下安装ElasticSearch6.2.4</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-7%E5%AE%89%E8%A3%85Head%E6%8F%92%E4%BB%B6"><span class="nav-number">1.1.7.</span> <span class="nav-text">1.7安装Head插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-8%E5%AE%89%E8%A3%85Kibana"><span class="nav-number">1.1.8.</span> <span class="nav-text">1.8安装Kibana</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-9%E5%AE%89%E8%A3%85%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8"><span class="nav-number">1.1.9.</span> <span class="nav-text">1.9安装中文分词器</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AC%AC%E4%BA%8C%E8%8A%82-ElasticSearch%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-number">1.2.</span> <span class="nav-text">第二节 ElasticSearch基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%80%92%E6%8E%92%E7%B4%A2%E5%BC%95"><span class="nav-number">1.2.1.</span> <span class="nav-text">2.1倒排索引</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2-%E5%80%92%E6%8E%92%E7%B4%A2%E5%BC%95%E5%8E%9F%E7%90%86"><span class="nav-number">1.2.1.1.</span> <span class="nav-text">2.1.2 倒排索引原理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-3-%E5%88%86%E8%AF%8D%E5%99%A8%E4%BB%8B%E7%BB%8D%E5%8F%8A%E5%86%85%E7%BD%AE%E5%88%86%E8%AF%8D%E5%99%A8"><span class="nav-number">1.2.1.2.</span> <span class="nav-text">2.1.3 分词器介绍及内置分词器</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E4%BD%BF%E7%94%A8ElasticSearch-API-%E5%AE%9E%E7%8E%B0CRUD"><span class="nav-number">1.2.2.</span> <span class="nav-text">2.2使用ElasticSearch API 实现CRUD</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E6%89%B9%E9%87%8F%E8%8E%B7%E5%8F%96%E6%96%87%E6%A1%A3"><span class="nav-number">1.2.3.</span> <span class="nav-text">2.3批量获取文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4%E4%BD%BF%E7%94%A8Bulk-API-%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E6%93%8D%E4%BD%9C"><span class="nav-number">1.2.4.</span> <span class="nav-text">2.4使用Bulk API 实现批量操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6"><span class="nav-number">1.2.5.</span> <span class="nav-text">2.5版本控制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-%E4%BB%80%E4%B9%88%E6%98%AFMapping"><span class="nav-number">1.2.6.</span> <span class="nav-text">2.6 什么是Mapping</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7%E5%9F%BA%E6%9C%AC%E6%9F%A5%E8%AF%A2-Query%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.7.</span> <span class="nav-text">2.7基本查询(Query查询)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-1%E6%95%B0%E6%8D%AE%E5%87%86%E5%A4%87"><span class="nav-number">1.2.7.1.</span> <span class="nav-text">2.7.1数据准备</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-2-term%E6%9F%A5%E8%AF%A2%E5%92%8Cterms%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.7.2.</span> <span class="nav-text">2.7.2 term查询和terms查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-3-%E6%8E%A7%E5%88%B6%E6%9F%A5%E8%AF%A2%E8%BF%94%E5%9B%9E%E7%9A%84%E6%95%B0%E9%87%8F"><span class="nav-number">1.2.7.3.</span> <span class="nav-text">2.7.3 控制查询返回的数量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-4-%E8%BF%94%E5%9B%9E%E7%89%88%E6%9C%AC%E5%8F%B7"><span class="nav-number">1.2.7.4.</span> <span class="nav-text">2.7.4 返回版本号</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-5-match%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.7.5.</span> <span class="nav-text">2.7.5 match查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-6-%E6%8C%87%E5%AE%9A%E8%BF%94%E5%9B%9E%E7%9A%84%E5%AD%97%E6%AE%B5"><span class="nav-number">1.2.7.6.</span> <span class="nav-text">2.7.6 指定返回的字段</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-7%E6%8E%A7%E5%88%B6%E5%8A%A0%E8%BD%BD%E7%9A%84%E5%AD%97%E6%AE%B5"><span class="nav-number">1.2.7.7.</span> <span class="nav-text">2.7.7控制加载的字段</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-8-%E6%8E%92%E5%BA%8F"><span class="nav-number">1.2.7.8.</span> <span class="nav-text">2.7.8 排序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-9-%E5%89%8D%E7%BC%80%E5%8C%B9%E9%85%8D%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.7.9.</span> <span class="nav-text">2.7.9 前缀匹配查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-10-%E8%8C%83%E5%9B%B4%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.7.10.</span> <span class="nav-text">2.7.10 范围查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-11-wildcard%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.7.11.</span> <span class="nav-text">2.7.11 wildcard查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-12-fuzzy%E5%AE%9E%E7%8E%B0%E6%A8%A1%E7%B3%8A%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.7.12.</span> <span class="nav-text">2.7.12 fuzzy实现模糊查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-7-13-%E9%AB%98%E4%BA%AE%E6%90%9C%E7%B4%A2%E7%BB%93%E6%9E%9C"><span class="nav-number">1.2.7.13.</span> <span class="nav-text">2.7.13 高亮搜索结果</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8-Filter%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.8.</span> <span class="nav-text">2.8 Filter查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8-1-%E7%AE%80%E5%8D%95%E7%9A%84%E8%BF%87%E6%BB%A4%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.8.1.</span> <span class="nav-text">2.8.1 简单的过滤查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8-2-bool%E8%BF%87%E6%BB%A4%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.8.2.</span> <span class="nav-text">2.8.2 bool过滤查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8-3-%E8%8C%83%E5%9B%B4%E8%BF%87%E6%BB%A4"><span class="nav-number">1.2.8.3.</span> <span class="nav-text">2.8.3 范围过滤</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8-5-%E8%BF%87%E6%BB%A4%E9%9D%9E%E7%A9%BA"><span class="nav-number">1.2.8.4.</span> <span class="nav-text">2.8.5 过滤非空</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-8-6-%E8%BF%87%E6%BB%A4%E5%99%A8%E7%BC%93%E5%AD%98"><span class="nav-number">1.2.8.5.</span> <span class="nav-text">2.8.6 过滤器缓存</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-9-%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.9.</span> <span class="nav-text">2.9 聚合查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-10-%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.10.</span> <span class="nav-text">2.10 复合查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-1-%E4%BD%BF%E7%94%A8bool%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.10.1.</span> <span class="nav-text">2.10.1 使用bool查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-2-constant-score%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.10.2.</span> <span class="nav-text">2.10.2 constant_score查询</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AC%AC%E4%B8%89%E8%8A%82-ElasticSearch%E5%8E%9F%E7%90%86"><span class="nav-number">1.3.</span> <span class="nav-text">第三节 ElasticSearch原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E8%A7%A3%E6%9E%90es%E7%9A%84%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84"><span class="nav-number">1.3.1.</span> <span class="nav-text">3.1 解析es的分布式架构</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84%E7%9A%84%E9%80%8F%E6%98%8E%E9%9A%90%E8%97%8F%E7%89%B9%E6%80%A7"><span class="nav-number">1.3.1.1.</span> <span class="nav-text">3.1.1 分布式架构的透明隐藏特性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-%E6%89%A9%E5%AE%B9%E6%9C%BA%E5%88%B6"><span class="nav-number">1.3.1.2.</span> <span class="nav-text">3.1.2 扩容机制</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-3-rebalance-%E8%87%AA%E5%8A%A8%E5%9D%87%E8%A1%A1"><span class="nav-number">1.3.1.3.</span> <span class="nav-text">3.1.3 rebalance(自动均衡)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-4-master%E8%8A%82%E7%82%B9"><span class="nav-number">1.3.1.4.</span> <span class="nav-text">3.1.4 master节点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-5-%E8%8A%82%E7%82%B9%E5%AF%B9%E7%AD%89"><span class="nav-number">1.3.1.5.</span> <span class="nav-text">3.1.5 节点对等</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E5%88%86%E7%89%87%E5%92%8C%E5%89%AF%E6%9C%AC%E6%9C%BA%E5%88%B6"><span class="nav-number">1.3.2.</span> <span class="nav-text">3.2 分片和副本机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E5%8D%95%E8%8A%82%E7%82%B9%E7%8E%AF%E5%A2%83%E4%B8%8B%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E5%88%86%E6%9E%90"><span class="nav-number">1.3.3.</span> <span class="nav-text">3.3 单节点环境下创建索引分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-%E4%B8%A4%E4%B8%AA%E8%8A%82%E7%82%B9%E7%8E%AF%E5%A2%83%E4%B8%8B%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E5%88%86%E6%9E%90"><span class="nav-number">1.3.4.</span> <span class="nav-text">3.4 两个节点环境下创建索引分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-%E6%B0%B4%E5%B9%B3%E6%89%A9%E5%AE%B9%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="nav-number">1.3.5.</span> <span class="nav-text">3.5 水平扩容的过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6ElasticSearch%E7%9A%84%E5%AE%B9%E9%94%99%E6%9C%BA%E5%88%B6"><span class="nav-number">1.3.6.</span> <span class="nav-text">3.6ElasticSearch的容错机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7%E6%96%87%E6%A1%A3%E7%9A%84%E6%A0%B8%E5%BF%83%E5%85%83%E6%95%B0%E6%8D%AE"><span class="nav-number">1.3.7.</span> <span class="nav-text">3.7文档的核心元数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-8-%E6%96%87%E6%A1%A3id%E7%94%9F%E6%88%90%E6%96%B9%E5%BC%8F"><span class="nav-number">1.3.8.</span> <span class="nav-text">3.8 文档id生成方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-9-source%E5%85%83%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90"><span class="nav-number">1.3.9.</span> <span class="nav-text">3.9 _source元数据分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-10-%E6%94%B9%E5%8F%98%E6%96%87%E6%A1%A3%E5%86%85%E5%AE%B9%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90"><span class="nav-number">1.3.10.</span> <span class="nav-text">3.10 改变文档内容原理解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-11-%E5%9F%BA%E4%BA%8Egroovy%E8%84%9A%E6%9C%AC%E6%89%A7%E8%A1%8Cpartial-update"><span class="nav-number">1.3.11.</span> <span class="nav-text">3.11 基于groovy脚本执行partial update</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-12-partial-update-%E5%A4%84%E7%90%86%E5%B9%B6%E5%8F%91%E5%86%B2%E7%AA%81"><span class="nav-number">1.3.12.</span> <span class="nav-text">3.12 partial update 处理并发冲突</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-13-%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E8%B7%AF%E7%94%B1%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90"><span class="nav-number">1.3.13.</span> <span class="nav-text">3.13 文档数据路由原理解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-14-%E6%96%87%E6%A1%A3%E5%A2%9E%E5%88%A0%E6%94%B9%E5%86%85%E9%83%A8%E5%8E%9F%E7%90%86"><span class="nav-number">1.3.14.</span> <span class="nav-text">3.14 文档增删改内部原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-15-%E5%86%99%E4%B8%80%E8%87%B4%E6%80%A7%E5%8E%9F%E7%90%86%E5%92%8Cquorum%E6%9C%BA%E5%88%B6"><span class="nav-number">1.3.15.</span> <span class="nav-text">3.15 写一致性原理和quorum机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-16-%E6%96%87%E6%A1%A3%E6%9F%A5%E8%AF%A2%E5%86%85%E9%83%A8%E5%8E%9F%E7%90%86"><span class="nav-number">1.3.16.</span> <span class="nav-text">3.16 文档查询内部原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-17-bulk%E6%89%B9%E9%87%8F%E6%93%8D%E4%BD%9C%E7%9A%84json%E6%A0%BC%E5%BC%8F%E8%A7%A3%E6%9E%90"><span class="nav-number">1.3.17.</span> <span class="nav-text">3.17 bulk批量操作的json格式解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-18-%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E5%88%86%E6%9E%90"><span class="nav-number">1.3.18.</span> <span class="nav-text">3.18 查询结果分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-19-%E5%A4%9Aindex%EF%BC%8C%E5%A4%9Atype%E6%9F%A5%E8%AF%A2%E6%A8%A1%E5%BC%8F"><span class="nav-number">1.3.19.</span> <span class="nav-text">3.19 多index，多type查询模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-20-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2%E4%B8%AD%E7%9A%84deep-paging%E9%97%AE%E9%A2%98"><span class="nav-number">1.3.20.</span> <span class="nav-text">3.20 分页查询中的deep paging问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-21-query-string%E6%9F%A5%E8%AF%A2%E5%8F%8Acopy-to%E8%A7%A3%E6%9E%90"><span class="nav-number">1.3.21.</span> <span class="nav-text">3.21 query string查询及copy_to解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-22%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8E%92%E5%BA%8F%E9%97%AE%E9%A2%98"><span class="nav-number">1.3.22.</span> <span class="nav-text">3.22字符串排序问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-23-%E5%A6%82%E4%BD%95%E8%AE%A1%E7%AE%97%E7%9B%B8%E5%85%B3%E5%BA%A6%E5%88%86%E6%95%B0"><span class="nav-number">1.3.23.</span> <span class="nav-text">3.23 如何计算相关度分数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-24-Doc-Values-%E8%A7%A3%E6%9E%90"><span class="nav-number">1.3.24.</span> <span class="nav-text">3.24 Doc Values 解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-25-%E5%9F%BA%E4%BA%8Escroll%E6%8A%80%E6%9C%AF%E6%BB%9A%E5%8A%A8%E6%90%9C%E7%B4%A2%E5%A4%A7%E9%87%8F%E6%95%B0%E6%8D%AE"><span class="nav-number">1.3.25.</span> <span class="nav-text">3.25 基于scroll技术滚动搜索大量数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-26-dynamic-mapping%E7%AD%96%E7%95%A5"><span class="nav-number">1.3.26.</span> <span class="nav-text">3.26 dynamic mapping策略</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-27%E9%87%8D%E5%BB%BA%E7%B4%A2%E5%BC%95"><span class="nav-number">1.3.27.</span> <span class="nav-text">3.27重建索引</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-28-%E7%B4%A2%E5%BC%95%E4%B8%8D%E5%8F%AF%E5%8F%98%E7%9A%84%E5%8E%9F%E5%9B%A0"><span class="nav-number">1.3.28.</span> <span class="nav-text">3.28 索引不可变的原因</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AC%AC%E5%9B%9B%E8%8A%82-%E5%9C%A8Java%E5%BA%94%E7%94%A8%E4%B8%AD%E8%AE%BF%E9%97%AEElasticSearch"><span class="nav-number">1.4.</span> <span class="nav-text">第四节 在Java应用中访问ElasticSearch</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1%E5%9C%A8Java%E5%BA%94%E7%94%A8%E4%B8%AD%E5%AE%9E%E7%8E%B0%E6%9F%A5%E8%AF%A2%E6%96%87%E6%A1%A3"><span class="nav-number">1.4.1.</span> <span class="nav-text">4.1在Java应用中实现查询文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E5%9C%A8Java%E5%BA%94%E7%94%A8%E4%B8%AD%E5%AE%9E%E7%8E%B0%E6%B7%BB%E5%8A%A0%E6%96%87%E6%A1%A3"><span class="nav-number">1.4.2.</span> <span class="nav-text">4.2 在Java应用中实现添加文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3%E5%9C%A8Java%E5%BA%94%E7%94%A8%E4%B8%AD%E5%AE%9E%E7%8E%B0%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3"><span class="nav-number">1.4.3.</span> <span class="nav-text">4.3在Java应用中实现删除文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4%E5%9C%A8Java%E5%BA%94%E7%94%A8%E4%B8%AD%E5%AE%9E%E7%8E%B0%E6%9B%B4%E6%96%B0%E6%96%87%E6%A1%A3"><span class="nav-number">1.4.4.</span> <span class="nav-text">4.4在Java应用中实现更新文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5%E5%9C%A8Java%E5%BA%94%E7%94%A8%E4%B8%AD%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E6%93%8D%E4%BD%9C"><span class="nav-number">1.4.5.</span> <span class="nav-text">4.5在Java应用中实现批量操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-6-%E6%9F%A5%E8%AF%A2%E5%88%A0%E9%99%A4"><span class="nav-number">1.5.</span> <span class="nav-text">4.6 查询删除</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-7-%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89"><span class="nav-number">1.6.</span> <span class="nav-text">4.7 查询所有</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-8-match-query-%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.7.</span> <span class="nav-text">4.8 match query 查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-9-multiMachQuery%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.8.</span> <span class="nav-text">4.9  multiMachQuery查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-10-term%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.9.</span> <span class="nav-text">4.10 term查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-11-terms%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.10.</span> <span class="nav-text">4.11 terms查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-12-%E5%85%B6%E4%BB%96%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.11.</span> <span class="nav-text">4.12 其他查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-13-%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.12.</span> <span class="nav-text">4.13 聚合查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-14-QueryString-%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.13.</span> <span class="nav-text">4.14 QueryString 查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-15-%E7%BB%84%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.14.</span> <span class="nav-text">4.15 组合查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-16-%E5%88%86%E7%BB%84%E8%81%9A%E5%90%88%EF%BC%88%E7%BB%9F%E8%AE%A1%EF%BC%89"><span class="nav-number">1.15.</span> <span class="nav-text">4.16 分组聚合（统计）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-17-%E8%BF%87%E6%BB%A4%E8%81%9A%E5%90%88"><span class="nav-number">1.16.</span> <span class="nav-text">4.17 过滤聚合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-18-range%E8%BF%87%E6%BB%A4"><span class="nav-number">1.17.</span> <span class="nav-text">4.18 range过滤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ES%E7%AE%A1%E7%90%86"><span class="nav-number">1.18.</span> <span class="nav-text">ES管理</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="王增明"
      src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">王增明</p>
  <div class="site-description" itemprop="description">喜欢折腾的极客聚集地.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbnplbmdtaW5n" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wanzengming"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <a href="/wangzengming@aliyun.com" title="E-Mail → wangzengming@aliyun.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王增明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">342k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:11</span>
</div>

        
<div class="busuanzi-count">
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script data-pjax>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


    <div id="pjax">
  

  

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script></body>
</html>
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
